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

私はあなたがこれを試してみたいと思う

$(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 に答える
0

ドキュメントには、No submit button value is serialized since the form was not submitted using a button..

とにかく、この回答のようにボタン名と値をserialize()出力に追加することでそれを行うことができます。

于 2013-07-09T04:38:52.663 に答える