2017-02-15 69 views
1

我有一个表单和ajax。如何用表格发送按钮值

我不能$(form).serialize();

如何按钮值与形式发送发送?

HTML:

<form action="" id="ogrenci_arama_formu"> 
      <input type="text" id="eposta" name="eposta"> 
      <button id="ogrenci_ara" name="ogrenci_ara" value="true" class="btn btn-info">Öğrenciyi Ara</button> 
      <!--<input id="ogrenci_ara" type="hidden" name="ogrenci_ara" value="true">--> 
     </form> 

AJAX:

$("#ogrenci_arama_formu").submit(function (e) { 
      e.preventDefault(); 
      console.log("form: ",$(this).serialize()); 
      $.ajax({ 
       url: "sayfalar/ogrenci_bilgileri.php", 
       type: 'post', 
       /*dataType: 'json',*/ 
       data: $(this).serialize() 
      }).done(function(data) { 
       $("tbody").html(data); 
      }).fail(function(data) { 
       console.log("error",data); 
      }); 
     }); 

输出:

eposta= 
+0

为什么你需要按钮值? –

+0

,因为我使用了两个按钮。一个名为seach按钮,一个名为excel-export按钮。 – bukalemun

回答

1

尝试像存储按钮值到一个变量中,并用形式发送序列化这样

Javascript成为

$("#ogrenci_arama_formu").submit(function(e) { 
    e.preventDefault(); 
    var btnValue = $(this).find('#ogrenci_ara').val(); 
    console.log("form: ", $(this).serialize()+'&ogrenci_ara='+btnValue); 
    $.ajax({ 
    url: "sayfalar/ogrenci_bilgileri.php", 
    type: 'post', 
    /*dataType: 'json',*/ 
    data: $(this).serialize()+'&ogrenci_ara='+btnValue 
    }).done(function(data) { 
    $("tbody").html(data); 
    }).fail(function(data) { 
    console.log("error", data); 
    }); 
}); 
+0

谢谢,但为什么我不能发送按钮值的形式? – bukalemun

+0

因为只有“成功的控件”被串行化为字符串,所以没有提交按钮值被序列化,因为表单没有使用按钮提交。你可以从链接[jQuery](http://api.jquery.com/serialize/)阅读更多关于'serialize()'方法的信息。 –

+0

@bukalemun如果答案可以帮助你,请将答案标记为已接受。 –