简体   繁体   中英

To submit a form in jquery by triggering one of sumbit button

If a form has two submit button, Then i need to submit a form by jquery as triggering submit of one of the submit button.

<?php
if(isset($_REQUEST['submit_button1']))
{
echo "form submitted by clicking submit button 1";
}
elseif(isset($_REQUEST['submit_button2']))
{
echo "form submitted by clicking submit button 2";
}

?>


<form name="check" action="">
<input type="text" name="text_val" id="text_val">
<input type="submit" name="submit_button1" value="submit button1">
<input type="submit" name="submit_button2" value="submit button2">
</form> 

question: $("form[name='check']").submit() submit the form in script. I need to submit the form by indicating that form is submitted by submit button 1 or submitted button2 in jquery.

Please help me in advance...

You can trigger click event

$('input[name="submit_button1"]').click();'

or

$('input[name="submit_button2"]').click();

您可以使用以下代码:

$("input[name='submit_button1']").click();

you can try something like this.

$("input[name="submit_button1"]").click(function(){

sendData('button1')

})

$("input[name="submit_button2"]").click(function(){

sendData('button2')


})

function sendData(button){

//i assume your for data like this
    var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone ;
//you can add your button as a parameter
  dataString += '&button='+ button

$.ajax({  
  type: "POST",  
  url: "yoururl",  
  data: dataString,  
  success: function() {  
    //succed
  }  
});  

}

and at server side first you can check button parameter then apply your logic.

I think you can call the "click" method on the button.

Try this...

$("form[name='check'] input[name='submit_button1']").click();

try:

$("form[name='check'] input[name=submit_button1]").click(); //or
$("form[name='check'] input[name=submit_button1]").trigger('click');

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