javascriptjqueryformsjqueryform

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>

Solution

  • <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();
    });