У меня есть групповые флажки, и мне нравится, если у этой группы есть поведение, такое как радиообъекты с одинаковым атрибутом имени.
Каждый флажок имеет другое имя.
Из флажков можно выбрать только один из них.
Как я могу это сделать?
Решение
Зачем мне это нужно? Потому что мы нуждаемся в согласованности webUI. Пожалуйста, это не вопрос о нашей архитектуре приложения.:)
Пример HTML
<div class="multiCheckBox">
<span class="multiGroup">
<div><input class="multiItem" value="111" name="list" type="checkbox" />111</div>
<div><input class="multiItem" value="112" name="list" type="checkbox" />112</div>
<div><input class="multiItem" value="113" name="list" type="checkbox" />113</div>
</span>
<span>
<div><input class="multiItem" value="121" name="list" type="checkbox" />121</div>
<div><input class="multiItem" value="122" name="list" type="checkbox" />122</div>
<div><input class="multiItem" value="133" name="list" type="checkbox" />123</div>
</span>
<span>
<div><input class="multiItem" value="131" name="list" type="checkbox" />131</div>
<div><input class="multiItem" value="132" name="list" type="checkbox" />132</div>
<div><input class="multiItem" value="133" name="list" type="checkbox" />133</div>
</span>
</div>
JavaScript
var $groups = $("span.multiGroup", $that);
$groups.each(function() {
var $group = $(this);
var $checkboxes = $(":checkbox", $group);
$checkboxes.click(function() {
var $activeCheckbox = $(this);
var state = $activeCheckbox.attr('checked');
$checkboxes.attr('checked', false);
$activeCheckbox.attr('checked', state);
});
});