我目前有一些 javascript/jquery 代码,如果没有选中任何框,则会显示错误消息。但是,当我选中一个框并提交表单时,仍然会显示错误消息。
任何帮助都会很棒!
javascript代码:
var hobbies = $('#hobbies').val();
if ($('#hobbies :checkbox:checked').length === 0){
$("#multichk").after('<span class="error">Please choose at least one.</span>');
hasError = true;
}
html代码:
<input type='checkbox' id='hobbies' name='hobbies[0]' value='1'/><label for='hobbies0'>football</label><br />
<input type='checkbox' id='hobbies' name='hobbies[1]' value='2'/><label for='hobbies1'>soccer</label><br />
<input type='checkbox' id='hobbies' name='hobbies[2]' value='3'/><label for='hobbies2'>baseball</label><br />
<input type='checkbox' id='hobbies' name='hobbies[3]' value='4'/><label for='hobbies3'>tennis</label><br />
<input type='hidden' id='multichk' />
谢谢你。