我需要使用 serialize() 函数发布按钮值。问题是 serialize() 不会发布按钮值。我的表单中有 SAVE、UPDATE 和 DELETE 按钮。然后 PHP 必须知道单击哪个按钮来执行特定操作。我可以使用 click 函数获取按钮值,但如何使用 Ajax 将其传递到 PHP 页面。我的代码在下面,也来自stackoverflow。
$(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');
}
});
});
});
});