我正在处理具有一组单选按钮选择的场景。 要求是只能选2个是。 因此,如果第三个也选择"is",我想将其恢复为“否”
此外,我还想知道是否可以只检查组内单选按钮的给定索引是否已检查,而不是这样做: “($(this).is(':checked') && $(this).val() == 'Y')”
jsFiddle链接:http://jsfiddle.net/pshah331/S8qep/
HTML:
<p>Folder A:
<input type="radio" name="folder_A" value="Y" onclick="folderSelectionChanged()" />Yes
<input type="radio" name="folder_A" value="N" onclick="folderSelectionChanged()" />No</p>
<p>Folder B:
<input type="radio" name="folder_B" value="Y" onclick="folderSelectionChanged()" />Yes
<input type="radio" name="folder_B" value="N" onclick="folderSelectionChanged()" />No</p>
<p>Folder C:
<input type="radio" name="folder_C" value="Y" onclick="folderSelectionChanged()" />Yes
<input type="radio" name="folder_C" value="N" onclick="folderSelectionChanged()" />No</p>
<div id="update"></div>
“HEAD”中的 JavaScript:
function folderSelectedYesCount() {
var yesCount = 0;
var folderRadioGroups = $("input[name^='folder_']");
folderRadioGroups.each(function () {
// TODO: Would like to check by index --> "index 0 is selected/checked"
if ($(this).is(':checked') && $(this).val() == 'Y') {
yesCount++;
}
});
return yesCount;
}
function folderSelectionChanged() {
// TODO: If count of Yes is greater than 2, then change the last clicked to "No"
/*
if (folderSelectedYesCount() > 1){
$(this)[1].prop('checked', true);
}
*/
$('#update').html('Selection count for "Yes" is <b>' + folderSelectedYesCount() + ' </b>');
}
最佳答案
我已经更新了你的 jsfiddle 请检查:-
function folderSelectionChanged(e) {
if(folderSelectedYesCount()>2){
$(e).siblings().prop("checked",true);
}
$('#update').html('Selection count for "Yes" is <b>' + folderSelectedYesCount() + ' </b>');
}
点击这里:- http://jsfiddle.net/S8qep/47/
谢谢
关于javascript - 如何使用 JavaScript 或最好是 jQuery 撤消单选按钮选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24914757/