简体   繁体   English

如何包含被单击的表单提交按钮的值?

[英]How to include value of form submit button that was clicked?

I must submit a form and I need to include the value of the submit button that was clicked. 我必须提交一个表单,并且需要包含单击的“提交”按钮的值。 How can I do that? 我怎样才能做到这一点? This is my code: 这是我的代码:

<form id="form_update" >
    <input type="text" class="form-control" name="title" value="" >
    <input type="hidden" name="prova" value="prova" />
    <button type="submit" name="salva" value="delete" class="btn btn-success">delete</button>
    <button type="submit" name="salva" value="modify" class="btn btn-success">modify</button>
    <button type="submit" name="salva" value="save" class="btn btn-success">save</button>
</form>
$(document).on('submit', '#form_update', function() { 
    return callAjax($(this).serialize()); 
}); 

function callAjax(data) {
    $.ajax({
        type: 'POST',
        url: 'call/function.php',
        data: data,
        success:  function(data) { 
            // code
        },
        error: function(data) { 
            alert('error'); 
        }
    });
    return false;
};  

I would change the event type to a click on the button and the access their values with $(this).val() 我将事件类型更改为单击按钮,然后使用$(this).val()访问其值。

$(document).on('click', 'button', function(e) {
    e.preventDefault(); // Prevent from submitting form. 
    var buttonValue = $(this).val();
    return callAjax($('#form_update').serialize()); 
}); 

Use the below function. 使用以下功能。

$(document).on('submit', '#form_update', function() { 
    var val = $("input[type=submit][clicked=true]").val();
    return callAjax($(this).serialize()+'&clickedVal='+val ); 
}); 

It will pass the clicked button value with the ajax request. 它将与ajax请求一起传递单击的按钮值。

Thanks... 谢谢...

With Arun solution I get undefined val value... 使用Arun解决方案,我得到未定义的val值...

With Daan solution the problem was the same... 使用大安解决方案,问题还是一样的...

But with your response I have resolved with a mix :) 但是根据您的回应,我已经解决了混合问题:)

$(document).on('click', 'button', function(e) {
  e.preventDefault(); // Prevent from submitting form. 
  var buttonValue = $(this).val();
  return callAjax($('#form_update').serialize()+'&salva='+buttonValue ); 
}); 

Thanks a lot ;) 非常感谢 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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