需要要求所有复选框组,直到选中一个
我正在使用 JQM (jQueryMobile),需要要求一组中的所有复选框,直到选择一个复选框,然后只要求选定的复选框,除非是全部选中然后要求全部。
问题是我无法使用复选框组的 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');
});
I'm using JQM (jQueryMobile) and need to require all check boxes in a group until one is selected, then just require the selected one unless it's the check all then require all.
The problem is I can't use the Id or Name attributes of the check box group, so I'm trying to use the class attribute.
Here is an example of the markup (but the functionality is not working): 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');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)