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

时间:2014-10-01 10:05:19

标签: jquery attr

我有这个问题,这段代码非常适合我的控件数组的第一次控制,但是如何将它应用于很多项目,例如" product_discount [1到n] [customer_group_id]"没有添加" id" ATTRIB?

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

thx andrea

2 个答案:

答案 0 :(得分:0)

您可以合并属性^(以...开头)和$(以结束)选择器

$('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);
});

答案 1 :(得分:0)

<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>