1

我需要使用 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');
                    }   
            }); 
        });
    });    
});
4

3 回答 3

3

使用它来获取按钮值,把它放在你有警报的地方:

var buttonvalue = $(this).val(); 

然后您可以通过执行以下操作将其附加到序列化数据中:

var values = $('#form1').serialize();
values = values + '&button='+buttonvalue;

'&button=' 是当您测试使用哪个按钮时将调用的。

于 2013-07-09T04:39:27.920 回答
0

文档指出No submit button value is serialized since the form was not submitted using a button..

无论如何,您可以通过将按钮名称和值附加到serialize()输出中来做到这一点,就像在这个答案中一样。

于 2013-07-09T04:38:52.663 回答
0

我想你想试试这个

$(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');
                    }   
            }); 
        });
    });    
});
于 2013-07-09T06:01:02.610 回答