【发布时间】:2012-01-13 12:48:56
【问题描述】:
我需要检查是否选中了复选框以显示错误消息。到目前为止,我认为这应该可以工作(如果没有检查,我认为它应该是空的)。
代码
<span id="sc_info" style="float: right; padding-right: 5px;">
<input type="checkbox" name="terms" id="sc_terms" value="" checked="checked" />
<br /><label class="error" for="body" id="terms_error">This field is required.</label>
</form>
<script>
$(document).ready(function() {
//Story Form
$('.error').hide();
$(".button").click(function() {
$('.error').hide();
var terms = $("input#sc_terms").val();
if (terms == "") { //if is not checked
$("label#terms_error").show(); //show error message
$("input#sc_terms").focus(); //focus on an empty element
return false;
}
});
});
</script>
但是这段代码不起作用。谁能建议如何检查复选框是否被选中?
【问题讨论】:
标签: javascript jquery html checkbox