Jquery 使用序列化函数发布提交按钮值以进行php验证
我需要使用serialize()函数发布按钮值。问题是serialize()不会发布按钮值。我的表单中有保存、更新和删除按钮。然后PHP必须知道单击哪个按钮可以执行特定操作。我可以使用click函数获取按钮值,但是如何使用Ajax将其传递到PHP页面。我的代码如下,也是从stackoverflow获得的Jquery 使用序列化函数发布提交按钮值以进行php验证,jquery,ajax,Jquery,Ajax,我需要使用serialize()函数发布按钮值。问题是serialize()不会发布按钮值。我的表单中有保存、更新和删除按钮。然后PHP必须知道单击哪个按钮可以执行特定操作。我可以使用click函数获取按钮值,但是如何使用Ajax将其传递到PHP页面。我的代码如下,也是从stackoverflow获得的 $(document).ready(function() { $("#btnUpdate").click(function() { alert(
$(document).ready(function()
{
$("#btnUpdate").click(function() {
alert($(this).attr("value"));
/* attach a submit handler to the form */
$("#form1").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/*clear result div*/
$("#result").html('');
/* get some values from elements on the page: */
var values = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "form.php",
type: "post",
data: values,
success: function(data){
alert("success");
alert(data);
$("#result").html('submitted successfully');
},
error:function(){
alert("failure");
$("#result").html('there is error while submit');
}
});
});
});
});
声明没有序列化任何提交按钮值,因为表单未使用按钮提交。
无论如何,您可以将按钮名称和值添加到
serialize()
输出中,如中所示。使用此按钮获取按钮值,将其放在出现警报的位置:
var buttonvalue = $(this).val();
然后,您可以通过执行以下操作将其附加到序列化数据:
var values = $('#form1').serialize();
values = values + '&button='+buttonvalue;
“&button=”是您测试使用哪个按钮时将调用的名称。我想您应该试试这个
$(document).ready(function()
{
$("#btnUpdate").click(function() {
/* attach a submit handler to the form */
$("#form1").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/*clear result div*/
$("#result").html('');
/* get some values from elements on the page: */
var values = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "form.php",
type: "post",
data: values + '&' + 'button=' $(this).attr('value'),
success: function(data){
alert("success");
alert(data);
$("#result").html('submitted successfully');
},
error:function(){
alert("failure");
$("#result").html('there is error while submit');
}
});
});
});
});
我认为相同rncrtr方法的可能重复没有问题。如果你不介意的话,请尽可能接受这个答案。谢谢