我正在尝试更改触发单选按钮的按钮值。
<fieldset id="product-color">
<input type="radio" id="red" name="color" value="Red">
<label for="red">Red</label><br>
<input type="radio" id="blue" name="color" value="Blue">
<label for="blue">Blue</label><br>
</fieldset>
<button
id="order-button"
data-item-id="1"
data-item-name="Shirt"
data-item-price="20"
data-item-custom2-name="Color"
data-item-custom2-options="Red|Blue">
Add Item
</button>
通过使用这个小脚本:
$('#product-color').change(function() {
$('#order-button').data('item-custom2-value', $(this).val());
});
对于选择输入字段,它工作得很好,但对于字段集则不行。有什么区别吗?
最佳答案
您需要更改单选按钮的事件,因为事件是在单选按钮而不是字段集上触发的:
$('#product-color input').change(function() {
$('#order-button').data('item-custom2-value', $(this).val());
});
<强> Working Demo
关于javascript - 字段集 .change jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41181263/