【问题标题】:How can i pass the object value to .on in jquery如何在 jquery 中将对象值传递给 .on
【发布时间】:2023-04-05 07:06:01
【问题描述】:

我有一个按钮,当使用单击它时,它会添加两个下拉列表,第二个的值取决于用户从第一个中选择的内容(值来自数据库),他可以根据需要添加多个下拉列表,所以我需要识别每个用户选择, 所以我这样做: 当他点击按钮添加时

    var Incremental = 0;
    $("#add-newProduct").on("click", function () {
        Incremental++;
        LoadCategories();
        var htmlString = `  
                            <tr>
                                <td>
                                    <select class="category-DropDownList`+ Incremental +`"> </select>
                                </td>

                                <td>
                                    <select class="product-DropDownList`+ Incremental +`"></select>
                                </td>

                                <td>
                                    <input id="product-price`+ Incremental +`" type="text" disabled />
                                </td>

                                <td>
                                    <input id="product-quantity`+ Incremental +`" type="text" disabled />
                                </td>

                                <td>
                                    <a class="delete-button`+ Incremental +` button is-danger">remove product</a>
                                </td>
                            </tr>

                        `;
        $("#product-rows").append(htmlString);
    });
   

function LoadCategories() {
        $.getJSON('@Url.Action("GetAllCategories", "Categories")', function (result) {
            console.log(result);
            $(".category-DropDownList" + Incremental).html("");
            $(".category-DropDownList" + Incremental).append("<option value=''>no thing selected</option>");
            for (var i = 0; i < result.length; i++) {
                $(".category-DropDownList" + Incremental).append("<option value=" + result[i].ID + ">" + result[i].Name + "</option");
            }
        });

然后当他从 .category-DropDownList 中选择一个值时

$("#product-rows").on("change", ".category-DropDownList" + Incremental, function () {
            showProducts($(this).val());
        });
        function showProducts(val)
        {
            console.log(val);
            $.getJSON('@Url.Action("GetProductsByCategoryId", "Products")' + "/" + val, function (result) {
                $(".product-DropDownList" + Incremental).html("");
                var data = result;
                console.log(data);
                $(".product-DropDownList" + Incremental).append("<option value=''>no thing selected</option>");
                for (var i = 0; i < data.length; i++) {
                    $(".product-DropDownList" + Incremental).append("<option value=" + data[i].ID + ">" + data[i].Name + "</option");
                }
            });
        }

所以问题在这里: $("#product-rows").on("change", ".category-DropDownList" + Incremental, function () 如何将增量值与 .category-DropDownList 连接起来

【问题讨论】:

  • 我认为您不必包含 + 增量部分,因为 $(this).val();选择器足以选择具有声明类的当前元素。
  • 如果我这样做,并使用 add htmlString 假设 5 次,那么每次他都会尝试从类别下拉列表中进行选择,这将影响所有产品下拉列表。
  • 为了防止这种情况,使用next('.classname'),这样它就不会影响同一个类的其他元素。另外,我建议在您的问题中添加一个 sn-p,以便我们对其进行测试。

标签: javascript jquery frontend web-frontend


【解决方案1】:

为每个元素使用一个公共类并使用它们出现的行来遍历同一行中的其他元素

当您开始删除行并且具有与增量不同的行数等时,您将遇到使用增量的问题。当您可以在所有行上通用地做事情时,这比值得做的事情要麻烦得多

由于我没有数据可用于创建选项,因此仅将产品的值更改为与类别相同的简单示例

$("#product-rows").on("change", ".category-DropDownList", function() {
  const cat = this.value,
    $row = $(this).closest('tr')
  showProducts(cat, $row);
});


function showProducts(cat, $row) {
  // fake ajax 
  Promise.resolve(cat).then(function(res) {
    $row.find('.product-DropDownList').val(res);
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="product-rows">
  <tr>
    <td><select class="category-DropDownList">
        <option value="">--- select Cat---</option>
        <option value="1">1</option>
        <option value="2">2</option>
      </select>
    </td>
    <td><select class="product-DropDownList">
        <option value=""></option>
        <option value="1">1</option>
        <option value="2">2</option>
      </select>
    </td>
  </tr>
  <tr>
    <tr>
    <td><select class="category-DropDownList">
        <option value="">--- select Cat---</option>
        <option value="1">1</option>
        <option value="2">2</option>
      </select>
    </td>
    <td><select class="product-DropDownList">
        <option value=""></option>
        <option value="1">1</option>
        <option value="2">2</option>
      </select>
    </td>
  </tr>
  <tr>
</table>

【讨论】:

  • 谢谢,工作正常,但仍然是一个小问题,当我单击按钮添加新产品(添加 htmlString)时,所有类别下拉列表都会重置。
  • 使用 .last() XD 解决它感谢您帮助我是前端新手,所以我很感激
  • 是的,完美...我要建议的内容
  • 请注意,这种模式在处理重复的元素组时非常常见....查找父容器实例...在该实例中查找
猜你喜欢
  • 2011-12-28
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多