简体   繁体   中英

submit form from dropdown menu text link

I have dropdown menu with some actions, but I need help to submit form on click via text link and pass the name attribute to the POST method. I have jQuery already on the page and code below works but name attribute is not sent.

<form id="myForm" name="myForm" action="" method="post">

<button class="dropdown">Actions</button>
<ul class="ul_dropdown">
     <li><a href="#" name="option1" onclick="$(this).closest('form').submit()">Option 1</a></li>
     <li><a href="#" name="option2" onclick="$(this).closest('form').submit()">Option 2</a></li>
</ul>
... other form fields ...
</form>

This is not how HTML works, a link doesnt send any information other than the url you set in the href attribute, you could do this

<li><a href="?name=option2" name="option2" >Option 2</a></li>

which does not require a form, or if you wish to Post you will need each option to be a seperate form, or you could use the solution provided by isherwood.

<ul class="ul_dropdown">
     <li><a href="#" name="option1">Option 1</a></li>
     <li><a href="#" name="option2">Option 2</a></li>
</ul>

<input id="linkName" type="hidden" />


$('ul_dropdown li a').on('click', function() {
    $('#linkName').val( $(this).attr('name') );
    $(this).closest('form').submit();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM