jquery按id从名称更改多个控件

jquery按id从名称更改多个控件,jquery,attr,Jquery,Attr,我有一个问题,这段代码对于我的控件数组的第一个控件非常有效,但是如何在不添加“id”属性的情况下将其应用于许多项目,如“产品折扣[1到n][customer\u group\u id]” thx andrea您可以将属性^(以开头)和$(以结尾)选择器组合在一起 $('select[name^="product_discount"][name$="customer_group_id"]').change(function () { var selected_discount = $('o

我有一个问题,这段代码对于我的控件数组的第一个控件非常有效,但是如何在不添加“id”属性的情况下将其应用于许多项目,如“产品折扣[1到n][customer\u group\u id]”


thx andrea

您可以将属性^(以开头)和$(以结尾)选择器组合在一起

$('select[name^="product_discount"][name$="customer_group_id"]').change(function () {
    var selected_discount = $('option:selected', this).attr("group_discount");
    $('input[name="product_discount[0][percentage_discount]"]').val(selected_discount);
});

违约
普罗瓦
%
里莫维
违约
普罗瓦
%
里莫维

你能分享htmlhi吗?它似乎不起作用。。但是,我还必须将元素的id[0到n.]传递到最后一行$('input[name=“product\u折扣[0][percentage\u折扣]”).val(选定的折扣);
$('select[name^="product_discount"][name$="customer_group_id"]').change(function () {
    var selected_discount = $('option:selected', this).attr("group_discount");
    $('input[name="product_discount[0][percentage_discount]"]').val(selected_discount);
});
<table class="list" id="discount">
                                   <tbody id="discount-row0">
              <tr>
                <td class="left"><select name="product_discount[0][customer_group_id]">
                                                            <option value="1" group_discount="12">Default</option>
                                                                                <option selected="selected" value="2" group_discount="50">prova</option>
                                                          </select></td>
                <td class="right"><input type="text" size="2" value="0" name="product_discount[0][quantity]"></td>
                <td class="right"><input type="text" size="2" value="0" name="product_discount[0][priority]"></td>
                <td class="right"><input type="text" value="3" name="product_discount[0][percentage_discount]"> %</td>
                <td class="right"><input type="text" value="0.0000" name="product_discount[0][price]"></td>
                <td class="left"><input type="text" class="date hasDatepicker" value="0000-00-00" name="product_discount[0][date_start]" id="dp1412160209055"></td>
                <td class="left"><input type="text" class="date hasDatepicker" value="0000-00-00" name="product_discount[0][date_end]" id="dp1412160209056"></td>
                <td class="left"><a class="button" onclick="$('#discount-row0').remove();">Rimuovi</a></td>
              </tr>
            </tbody>
                                    <tbody id="discount-row1">
              <tr>
                <td class="left"><select name="product_discount[1][customer_group_id]">
                                                            <option selected="selected" value="1" group_discount="12">Default</option>
                                                                                <option value="2" group_discount="50">prova</option>
                                                          </select></td>
                <td class="right"><input type="text" size="2" value="0" name="product_discount[1][quantity]"></td>
                <td class="right"><input type="text" size="2" value="0" name="product_discount[1][priority]"></td>
                <td class="right"><input type="text" value="3" name="product_discount[1][percentage_discount]"> %</td>
                <td class="right"><input type="text" value="0.0000" name="product_discount[1][price]"></td>
                <td class="left"><input type="text" class="date hasDatepicker" value="0000-00-00" name="product_discount[1][date_start]" id="dp1412160209057"></td>
                <td class="left"><input type="text" class="date hasDatepicker" value="0000-00-00" name="product_discount[1][date_end]" id="dp1412160209058"></td>
                <td class="left"><a class="button" onclick="$('#discount-row1').remove();">Rimuovi</a></td>
              </tr>
          </table>