Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

submit nameless form with jquery anchor

I am trying to use a link to submit a form, the kicker is I do not know the form name. I can not seem to navigate to my form object.

$('a#deleteRecord').click(function(){
var $target = $(this).parent().next('form');
$target.submit();
}); 

The from is.

<form method="post" action=" <?php echo $_SERVER['PHP_SELF'] ?>" >
<input type="hidden" name="action" value="delete"/>
<input type="hidden" name="record" value="1"/>
<a id="deleteRecord" class="btn btn-danger" href="#" ><i class="fa fa-trash-o"></i></a> 

Because I am creating the form from a database it is not practical to name the form. Can someone please help me with this code, or suggest a better solution. Thank you

like image 246
Rex Avatar asked Jan 24 '26 13:01

Rex


1 Answers

You do not need next(), the form is direct parent of your anchor so parent() would be enough. The form tag in html is not closed, you need to add </form> to close form tag.

Live Demo

var $target = $(this).parent();

Get the parent of each element in the current set of matched elements, optionally filtered by a selector, jQuery Doc.

If your want to find the parent multiple levels up in the DOM hierarchy then using closest() is quite handy.

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree, jQuery doc.

like image 166
Adil Avatar answered Jan 26 '26 03:01

Adil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!