多个AJAX请求只有第一个工作其他失败

时间:2015-10-25 14:57:31

标签: javascript jquery ajax

我有多个输入,对于每个输入我都有一个单独的AJAX请求。



< script type = "text/javascript" >
  $(document).ready(function() {
    $("#id_1").change(function() {
      var rating1 = $(this).val();
      //                            alert(rating1);
      $.ajax({
        url: "a.php",
        type: "post",
        data: {
          param_name: rating1
        },
        success: function(response) {
          //                                    alert(response);
          $("#message").html(response);
          // you will get response from your php page (what you echo or print)
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.log(textStatus, errorThrown);
        }
      });
    });
  }); < /script>
&#13;
&#13;
&#13;

输入代码是

&#13;
&#13;
<p style="text-align: center;font-size: 15pt;">
  <input type="number" name="your_awesome_parameter" id="id_1" class="rating" data-clearable="remove" data-icon-lib="fa" data-active-icon="fa-heart" data-inactive-icon="fa-heart-o" data-clearable-icon="fa-trash-o" />
</p>
&#13;
&#13;
&#13;

我在具有唯一ID的单独div中有多个输入,并且这些ID在AJAX代码中也是唯一的。当我运行它时,只有第一个可以工作但第二个没有。

第二次输入的代码

&#13;
&#13;
<script type="text/javascript">
  $(document).ready(function() {
    $("#id_2").change(function() {
      var rating1 = $(this).val();
      //                            alert(rating1);
      $.ajax({
        url: "a.php",
        type: "post",
        data: {
          param_name: rating1
        },
        success: function(response) {
          //                                    alert(response);
          $("#message").html(response);
          // you will get response from your php page (what you echo or print)
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.log(textStatus, errorThrown);
        }
      });
    });
  });
</script>
&#13;
<p style="text-align: center;font-size: 15pt;">
  <input type="number" name="your_awesome_parameter" id="id_2" class="rating" data-clearable="remove" data-icon-lib="fa" data-active-icon="fa-heart" data-inactive-icon="fa-heart-o" data-clearable-icon="fa-trash-o" />
</p>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

感谢@charlietfl的建议我对代码进行了一些更改,例如现在它查看了类,因此我只需要使用一次代码,它也会得到字段的id以及输入,以便我可以知道它来自哪个输入

&#13;
&#13;
< script type = "text/javascript" >
  $(document).ready(function() {
    $(".rating").change(function() {
      var rating1 = $(this).val();
      var inputId = $(this).attr("id");
      alert(inputId);
      $.ajax({
        url: "a.php",
        type: "post",
        data: {
          param_name: rating1,
          id: inputId
        },
        success: function(response) {
          //                                    alert(response);
          $("#message").html(response);
          // you will get response from your php page (what you echo or print)
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.log(textStatus, errorThrown);
        }
      });
    });
  }); < /script>
&#13;
&#13;
&#13;