2016-12-16 57 views
1

我試圖改變按鈕值觸發一個單選按鈕。Fieldset .change jQuery

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

隨着它工作的罰款選擇輸入字段,但不與一個字段。有什麼區別嗎?

+0

OP是有點不清楚,你想改變按鈕值事件單擊或單選按鈕更改。您還希望將哪個值指定爲字段集的數據值? – guradio

回答

1

您需要的單選按鈕更改事件作​​爲事件的單選按鈕發射,而不是字段集:

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

Working Demo