【问题标题】:jQuery send post variable using ajax and submit formjQuery使用ajax发送post变量并提交表单
【发布时间】:2014-01-20 12:09:39
【问题描述】:

我有带有动态字段数的 html 表单,例如:

<form id="myform">
<input type="text" id="input1">
<input type="text" id="input2">
<input type="text" id="input3">
...
<input type="text" id="inputN">
<span id="button_click"> CLICK </span>
</form>

和 jQuery 是:

  $("#button_click").click(function(){

    $.post("myfile.php",
    {
      XXXX:YYYY
    },
    function(data,status){
      // do anyting
    });

  });

我不知道确切的字段数,所以我无法填写 XXXX - 发布变量名称和 YYYY - 网页中的字段数据....所以我无法一一计数/写入。 ..

如何使用 AJAX 和点击按钮通过 post 变量提交整个表单?

【问题讨论】:

    标签: php jquery ajax forms submit


    【解决方案1】:

    听起来您正在寻找.serialize() 方法:

    $.post("myfile.php",
    $("#myform").serialize(),
    function(data,status){
        // do anyting
    });
    

    http://api.jquery.com/serialize/

    【讨论】:

      【解决方案2】:
      //rough code for general puporse of storing values for post
      var obj = $("#myform"),
      data = {};//to hold the values
      obj.find('[name]').each(function(index, value) {
          name = obj.attr('name'),
          value = obj.val();
          data[name] = value;
      });
      $.post("myfile.php",
          data: data,
          function(data,status){
            // do anyting
          });
      

      【讨论】:

      • 非常感谢您的决定,这是获取数据并通过 post 变量发送数据的好方法。我会试试...谢谢!
      猜你喜欢
      • 1970-01-01
      • 2014-08-15
      • 2014-01-22
      • 2016-07-08
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多