需要要求所有复选框goup,直到选中一个
问题描述:
我正在使用JQM(jQueryMobile),并且需要选中组中的所有复选框,直到选中一个复选框,然后只需要选中一个复选框,除非它是全部检查所有。需要要求所有复选框goup,直到选中一个
问题是我不能使用复选框组的Id或Name属性,所以我试图使用class属性。
这里是标记的例子(但功能不工作):http://jsfiddle.net/HfLq3/7/
HTML:
<div data-role="page">
<form id="chips_form" name="chips_form" action="#" method="post">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose as many snacks as you'd like:</legend>
<input type="checkbox" name="checkbox-1a" id="checkbox-1a" class="custom chips" />
<label for="checkbox-1a">Cheetos</label>
<input type="checkbox" name="checkbox-2a" id="checkbox-2a" class="custom chips" />
<label for="checkbox-2a">Doritos</label>
<input type="checkbox" name="checkbox-3a" id="checkbox-3a" class="custom chips" />
<label for="checkbox-3a">Fritos</label>
<input type="checkbox" name="checkbox-4a" id="checkbox-4a" class="custom chips" />
<label for="checkbox-4a">Sun Chips</label>
<input type="checkbox" name="checkbox-5a" id="checkbox-5a" class="custom chips" />
<label for="checkbox-5a">Select All</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<button type="submit" name="submit" value="chips-submit">Submit</button>
</div>
</form>
</div>
JS:
$(".chips").change(function() {
alert('selected Value(s): '+$(this).val());
});
$('.chips').each(function() {
$(this).attr('required','required');
});
答
$(".chips").change(function() {
$('.chips').attr('required', $(this).attr("checked") ? '': 'required');
});
$('.chips').attr('required','required');
它的工作原理,除非我检查,然后取消检查,表格仍然提交没有检查 – 2011-03-31 19:22:28
嗯,工作正常。检查http://jsfiddle.net/HfLq3/9/ – 2011-03-31 19:28:01