選擇複選框,我有以下的HTML:jQuery的:基於名稱和值
<form id="test">
<input type="radio" value="A" name="C1"/>
<a href="javascript:selectCheckbox('C1', 'A');"> OPTION-A </a>
<input type="radio" value="B" name="C1"/>
<a href="javascript:selectCheckbox('C1', 'B');"> OPTION-B </a>
<input type="radio" value="C" name="C1"/>
<a href="javascript:selectCheckbox('C1', 'C');"> OPTION-C </a>
// several other: C2, C3, ..
</form>
而且我想實現selectCheckbox(chkbox, value)
,這應該:
- 搜索所有無線電與
name = chkbox
和設置attr('checked') = false
- 搜索收音機有
name = chkbox AND val() = value
和設置attr('checked') = true
我想不通,正確的選擇是什麼,我想沒有任何運氣如下:
var name = "#" + chkbox + " :checked";
$(name).each(.. // doesn't work
$('#'+chkbox).each(.. // if finds only the first occurence
// of i.e. C1, although I would expect 3
$("input[@name='"+chkbox+"']").each(function() { ..
// leaves me with the following error:
// Warning: Expected attribute name or namespace but found '@name'
請讓我知道我做錯了。非常感謝!
的代碼你不應該有相同的ID(「C1」)的多個元素 – Zed 2009-09-19 17:13:35
你說得對,我刪除了ID現在。謝謝! – MrG 2009-09-19 17:15:41
「注意:在jQuery 1.3 [@attr]樣式選擇器被刪除(他們以前在jQuery 1.2中被棄用)。「 – Zed 2009-09-19 17:17:19