2011-08-19 51 views
1

我有我的頁面上的單選按鈕組:jQuery新手:如何在我的情況下更新單選按鈕值?

<input type="radio" name="fruit" value="apple" checked> apple 
<input type="radio" name="fruit" value="orange"> orange 
<input type="radio" name="fruit" value="banana"> banana 

如果我要動態地更新單選按鈕的外觀值,該怎麼辦呢jQuery的(或什麼是最好的方式在jQuery中完成)?我使用jQuery 1.5.1

我的意思是,例如,從更新的值:

蘋果」, 「橙色」 和 「香蕉

西瓜」,「」和「草莓」 。

回答

4

首先,你將需要添加值屬性每個單選按鈕

<input type="radio" name="fruit" value="apple" checked> apple 
<input type="radio" name="fruit" value="orange"> orange 
<input type="radio" name="fruit" value="banana"> banana 

然後你可以使用jquery與

$("input[value='apple']").val("Melon"); 

更新值在理想情況下,你會想添加一個獨特的標識符到每個單選按鈕,例如

<input id="option1" type="radio" name="fruit" value="apple" checked="checked"> <label for="option1">apple</label> 
<input id="option2" type="radio" name="fruit" value="orange" checked="checked"> <label for="option2">orange</label> 
<input id="option3" type="radio" name="fruit" value="banana" checked="checked"> <label for="option3">banana</label> 

通過向每個o添加一個id F中的單選按鈕可以讓你做到以下幾點:

$("#option1").val("Melon"); //This will update the radio button value 
$("label[for='option1']").html("Melon"); //This will update the label value 

而且在標籤標籤包裝標籤使得他們點擊來選擇相關的單選按鈕

+0

我已經更新上面也更新標籤字段 – Leo

+0

嗨,標籤不會更新通過使用您的代碼,只更新單選按鈕值。 – Leem

+0

刪除jquery調用的標籤部分之前的#,以上更新... – Leo

相關問題