将从AJAX调用的内容用于另一个AJAX调用

时间:2017-11-12 05:36:01

标签: javascript php jquery html ajax

在我的表单将被操作的页面上,我在加载页面后立即执行以下AJAX调用。我想要一份供应商和产品ID的下拉列表。此AJAX调用引用连接到数据库的URL并提取所有当前ID值,并将它们放入<option>下拉元素下的<select>标记中。

$(function(){
    // this is getting the dropdown values for the Supplier ID dropdown
    $.ajax({
      url: "getSuppliers.php",
      async: true,
      success: function(result) {
        $("#supplier").html(result);
      }
    })


    // this is getting the dropdown values for the Product ID dropdown
    $.ajax({
      url: "getProducts.php",
      async: true,
      success: function(result) {
        $("#products").html(result);
      }
    })

  });

对我来说,下一步是调用AJAX在此下拉项目更改时执行某些操作。我已对以下引用的<select>getSuppliers.php中的getProducts.php元素执行了以下操作。

  <option name="supplierID" value=$row["SupplierID"] onchange="suppChange()"> $row["SupplierID"] </option>";

当调用此下拉列值时,我正在调用函数suppChange()。我在我的表单所在的主文件中有suppChange()(我前两个AJAX调用的文件相同)。 suppChange()看起来像这样:

  function suppChange() {
      $.ajax({
        url: "displaySuppliers.php",
        async: true,
        success: function(result) {
          $("#supplierStatic").html(result);
          $("#products").html("TESTING");
        }
      })
  }

当我更改表单时,没有任何反应。它与无效的<option>无关,因为我尝试使用console.log(supplierID)获取此值并获得正确的值。我只是想知道为什么onchange=suppChange()没有做任何事情并在我更改下拉列表时调用该函数。

1 个答案:

答案 0 :(得分:2)

onchange应该在select代码中使用,而不是option代码

https://www.w3schools.com/jsref/event_onchange.asp