【问题标题】:Fieldset .change jQuery字段集 .change jQuery
【发布时间】:2016-12-16 09:38:31
【问题描述】:

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

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

使用 select-input-field 可以很好地工作,但不能使用 fieldset。有什么区别吗?

【问题讨论】:

  • OP 有点不清楚您要更改按钮单击或单选按钮更改时的值的事件。您还想将哪个值分配为字段集的数据值?

标签: javascript jquery forms onchange fieldset


【解决方案1】:

您需要单选按钮的更改事件,因为事件是在单选按钮而不是字段集上触发的:

$('#product-color input').change(function() {
   $('#order-button').data('item-custom2-value', $(this).val());
});

Working Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 2020-12-23
    • 2012-10-09
    • 2011-04-10
    相关资源
    最近更新 更多