javascript - 字段集 .change jQuery

标签 javascript jquery forms onchange fieldset

我正在尝试更改触发单选按钮的按钮值。

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

相关文章:

javascript - Dreamweaver 缩小插件

javascript - 单击图像时弹出带有信息的窗口

javascript - 如何在文本区域中从右到左书写英文

javascript - JQuery Mobile 重复的 Js/Js 在其他页面中不起作用

javascript - Google Adword Api 与 php 集成

jquery - 如何在我的网络应用程序中的谷歌地图上显示多个位置

jquery - 使用 Meteor、jQuery 和 AWS SDK 将浏览器直接上传到 S3

javascript - React - 输入值在提交时清除

javascript - 将事件绑定(bind)到没有事件的 <label> 然后触发两次

javascript - Gmail 样式始终可见的菜单