要加载的 jQuery 发布参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1330495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:14:33  来源:igfitidea点击:

jQuery post parameter to load

jquerypostload

提问by Charles Harmon

How do I pass the values of txtname and tel as variables to the .load???

如何将 txtname 和 tel 的值作为变量传递给 .load ???

$(document).ready(function(){
    $("#add").click(function(){
        $("#result").load("add.php", {name: #txtname});
    });
});

The html:

html:

<p>Name:<input type="text" name="name" value="" id="txtname" /></p>
<p>Telephone:<input type="text" name="tel" id="tel" value="" /></p>
<input type="submit" value="Submit" id="add" />

回答by Tim S. Van Haren

$(document).ready(function() {
    $("#add").click(function() {
       $("#result").load("add.php", {
           name: $("#txtname").val(), 
           tel: $("#tel").val()
       });
    });
});

回答by Charles Harmon

I've found a good way to do this - use serializeArray() for the data part, if you are pulling from a form and still want to use .load(). It may save you some extra work.

我找到了一个很好的方法来做到这一点 - 如果您从表单中提取并且仍然想要使用 .load(),则将 serializeArray() 用于数据部分。它可能会为您节省一些额外的工作。

var form_data = $('#my-form').serializeArray();
$('.my-container').load('/myurl/', form_data);

回答by Brynner Ferreira

$('.element').load('page.php', {var1:'value1', var2:'value2'}, function() {
// actions after load page (optional)
});

回答by Sampson

$(document).ready(function(){
  $("#add").click(function(){
    $("#result").load("add.php", {
      'name': $("#txtname").val(), 
      'telephone': $("#tel").val()
    });
  });
});

回答by Pierre de LESPINAY

You can also wrap your inputs into a form

您还可以将您的输入包装成一个表单

<form action="add.php" method="GET" id="my_form">
  <p>Name:<input type="text" name="name" value="" id="txtname" /></p>
  <p>Telephone:<input type="text" name="tel" id="tel" value="" /></p>
  <input type="button" value="Submit" id="add" />
</form>

So you can easily maintain your parameters

因此您可以轻松维护您的参数

$(document).ready(function() {
  $("#add").click(function() {
    $("#result").load(
      $("my_form").attr('action')
    , $("my_form").serialize()
    });
  });
});