简体   繁体   English

如何将具有相同类和名称的多个输入字段传递给ajax请求并根据回复更改相应的字段?

[英]How to pass multiple input fields having same class and name to ajax request and change corresponding fields based on reply?

I have a form like below; 我的表格如下:

<form>
  <table>
    <tr><td><input class="asdf" type="text" name="qwerty" value=""/></td></tr>
    <tr><td><input class="items" type="text" name="item" value=""/></td></tr>
    <tr><td><input class="items" type="text" name="item" value=""/></td></tr>
    <tr><td><input class="items" type="text" name="item" value=""/></td></tr>
    <tr><td><input class="items" type="text" name="item" value=""/></td></tr>
    <tr><td><button id="mybutton">clickme</button></td></tr>
  </table>
</form>

I have some fields with similar name. 我有一些名称相似的字段。 I want to pass these into my ajax handler in a single instance. 我想在一个实例中将它们传递到我的ajax处理程序中。 Also according to the ajax reply, the corresponding input field's value may be changed to either true or false. 同样根据ajax答复,相应的输入字段的值可以更改为true或false。

$('#mybutton').click(function(){
  var myitem = $('.items').val();
  $.ajax({
    type: 'POST',
    url: 'ajaxhandler.php',
    data: 'qwerty='+$('.asdf').val()+'&item='+myitem,
    cache: false,
    success: function(result) {
      if(result == "true"){
        //change each correspinding input field to true
      }else{
        //change each correspinding input field to false
      }
    }
  });
});

Also I am little confused how to do the ajax reply. 另外我也不太困惑如何做ajax回复。 How can I achieve this? 我该如何实现? Below is an image showing the form before and after ajax (what I am trying to achieve); 下图显示了ajax之前和之后的形式(我正在尝试实现的形式);

需求

The input form fields (items) are generated dynamically. 输入表单字段(项目)是动态生成的。 Sometimes there may be 1, sometimes 5 or 10. So it is not possible to give them separate names (like item1, item2 etc) and a common class, and catch them inside php like $_POST['item1'] , $_POST['item2'] etc. I want to know how to set the JavaScript as well as the ajax PHP. 有时可能有1,有时是5或10。因此,不可能给它们分别命名(如item1,item2等)和一个通用类,并像$_POST['item1']$_POST['item2'] _ POST $_POST['item2']等。我想知道如何设置JavaScript以及ajax PHP。

Try with this 试试这个

<form>
<table>
    <tr><td><input class="asdf" type="text" name="qwerty" value="0"/></td></tr>
    <tr><td><input class="items" type="text" name="item[]" value="1"/></td></tr>
    <tr><td><input class="items" type="text" name="item[]" value="2"/></td></tr>
    <tr><td><input class="items" type="text" name="item[]" value="3"/></td></tr>
    <tr><td><input class="items" type="text" name="item[]" value="4"/></td></tr>
    <tr><td><button id="mybutton">clickme</button></td></tr>
  </table>
</form>

Script 脚本

$('form').serialize()

In your code 在你的代码中

$('#mybutton').click(function(){
  var mydata = $('form').serialize()
  // it will return like this qwerty=0&item%5B%5D=1&item%5B%5D=2&item%5B%5D=3&item%5B%5D=4
  $.ajax({
    type: 'POST',
    url: 'ajaxhandler.php',
    data: mydata,
    cache: false,
    success: function(result) {
      if(result == "true"){
        //change each correspinding input field to true
      }else{
        //change each correspinding input field to false
      }
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM