在我的表单上有一组单选按钮。这是标记:
<div class="optionHolder">
<p class="optionName">Format</p>
<div class="option checked">
<input type="radio" name="fileType" value="avi" />
<img src="images/avi.png" alt="" />
<label>AVI</label>
</div>
<div class="option">
<input type="radio" name="fileType" value="mov" />
<img src="images/mov.png" alt="" />
<label>MOV</label>
</div>
<div class="option">
<input type="radio" name="fileType" value="mp4" />
<img src="images/mp4.png" alt="" />
<label>MP4</label>
</div>
<div class="option">
<input type="radio" name="fileType" value="mp3" />
<img src="images/mp3.png" alt="" />
<label>MP3</label>
</div>
</div>
提交表单后,我想检查其中之一是否已选中。解决这个问题的最佳方法是什么?我正在考虑循环遍历它们,并设置一个标志来设置是否检查其中一个,然后在循环后检查该标志,如果为假则抛出错误。
任何帮助表示赞赏,干杯。
您可以使用length and 相等属性选择器 with :checked过滤器选择器如下所示:
if ($("input[name='fileType']:checked").length > 0){
// one ore more checkboxes are checked
}
else{
// no checkboxes are checked
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)