How to include submit button name and values on form Serialize() ajax

后端 未结 2 596
余生分开走
余生分开走 2021-01-22 01:01

I have trouble, my code doesn\'t work, because my server script side need a name from the submit button. I\'m using Ajax method, and I\'m using data: serialize, when I have Clic

相关标签:
2条回答
  • 2021-01-22 01:22

    I believe you can't do that but you can use a hidden field

    <input type="hidden" name="any_name" value="any_value" />
    
    0 讨论(0)
  • 2021-01-22 01:34

    Try this:

    Java Script Code:

    <script type='text/javascript'>
            $(function(){
                $('#form').submit(function(e){
                    e.preventDefault();
                    var submitData = $('#form').serialize();
                    var btnName = $('#submit').attr('name');
                    var btnVal = $('#submit').val();
                    var btn = '&'+btnName+'='+btnVal;
                    submitData += btn;
                    alert(submitData);                
    
                }); 
            }); 
    </script>
    

    HTML Code:

    <form action="" id="form" type='post'>
        <input type="text" name="name" id="name" value='Scott'/>
        <input type="submit" name="submit" id="submit" value='POST'/>
    </form>
    

    or use:

    var submitData = $('#buy_product').serialize();
    submitData += '&btnName=' +  $('#your_submitbtn_id').attr('name') + '&btnValue='+ $('##your_submitbtn_id').attr('value'); 
    
    0 讨论(0)
提交回复
热议问题