我需要能够判断是否选中了复选框以进行一些基本验证。问题:我无权访问生成此内容的 PHP。没有添加类,或者大多数表单都有的基本的checked=checked。定位复选框的最简单方法是什么?
http://www.inpresence.in/event-registration?ee=4
编辑:吓坏了!这是代码,我只需要定位复选框,其他一切都正常。 jquery 的 :checked 方法在复选框中使用了checked=checked,但该复选框不存在。
$(document).ready(function(){
//when the submit button is clicked...
$("input.btn_event_form_submit").click(function(){
//find the value of the drop down with one evening or four evenings
var priceOption = $("#price_option-4").val();
//match a string ending with "one evening" as the first numbers will be randomly generated by php
var oneEvening = /^\d{2}\|One Evening$/.test(priceOption);
//match a string ending with "four evenings" as the first numbers will be randomly generated by php
var fourEvenings = /^\d{2}\|Four Evenings$/.test(priceOption);
//HOW DO I GET THE CHECKED BOXES?!
var checkedBoxCount = $('#dates-1351733097 .valid').is(':checked').length;
//if one evening is selected make sure the checked boxes count does in fact equal one
if(oneEvening && checkedBoxCount != 1){
//if it doesn't alert the user and return false
alert('You must select one date');
return false;
}
//if one evening isn't selected, four is. make sure the count does indeed in 4
else if (fourEvenings && checkedBoxCount != 4){
//if it doesnt alert the user and return to the form
alert('You must select four dates');
return false;
}
//else, everything checks out!
else {
return;
}
});
});
最佳答案
使用此 JavaScript 代码,您可以检查复选框是否已选中:
var isChecked = document.getElementById("my-checkbox").checked;
或者使用 jQuery:
var isChecked = $('#my-checkbox').is(':checked');
编辑:尝试一下并告诉我它是否有效:
var checkedBoxCount = $('#dates-1351733097 .valid:checked').length;
关于javascript - 我如何判断复选框是否已选中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13237692/