Submit anonymous form using jquery anchor

I am trying to use a link to submit a form, the kicker is not the name of the form. I can't seem to go to my form object.

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

From.

<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> 

Since I am creating a form from the database, you cannot name the form. Can someone please help me with this code or suggest a better solution. Thanks you

+3
source share
2 answers

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

Live demo

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

, , jQuery Doc.

DOM, closeest() .

, DOM, jQuery doc.

+4

. closeest()

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

All Articles