Javascript Can';t使用自定义jQuery表单下拉验证选择GetElementByName的值
我正在使用并尝试验证两个时间字段。我想确保如果一个字段选择了“All”,则另一个字段为真,Javascript Can';t使用自定义jQuery表单下拉验证选择GetElementByName的值,javascript,jquery,jquery-plugins,jquery-validate,getelementsbyname,Javascript,Jquery,Jquery Plugins,Jquery Validate,Getelementsbyname,我正在使用并尝试验证两个时间字段。我想确保如果一个字段选择了“All”,则另一个字段为真,结束时间大于开始时间 以下是HTML: <form id="schedule"> <select name='start_hour' id='start_hour'> <option value='All00'>All</option> <option value='0000'>00</option> <
结束时间
大于开始时间
以下是HTML:
<form id="schedule">
<select name='start_hour' id='start_hour'>
<option value='All00'>All</option>
<option value='0000'>00</option>
<option value='0100'>01</option>
<option value='0200'>02</option>
<option value='0300'>03</option>...
</select>
and
<select name='end_hour' id='end_hour'>
<option value='All00'>All</option>
<option value='0000'>00</option>
<option value='0100'>01</option>
<option value='0200'>02</option>
<option value='0300'>03</option>...
</select>
</form>
全部的
00
01
02
03...
及
全部的
00
01
02
03...
以下是自定义规则:
jQuery.validator.addMethod( "schedule", function(value, element) {
var start_hour = document.getElementsByName("start_hour");
var end_hour = document.getElementsByName("end_hour");
alert(start_hour.value);
alert(end_hour.value);
if (start_hour.value == "All00" && end_hour.value !="All00")
{
alert('end hour all error')
return false;
}
else if (end_hour.value == "All00" && start_hour.value !="All00")
{
alert('start hour all error')
return false;
}
else if (end_hour.value <= start_hour.value ){
alert('end hour must be larger error')
return false;
}
else return true;
}, "Error with schedule");
jQuery.validator.addMethod(“schedule”,函数(值,元素){
var start_hour=document.getElementsByName(“start_hour”);
var end_hour=document.getElementsByName(“end_hour”);
警报(开始时间值);
警报(结束时间值);
如果(开始时间值==“All00”&&end时间值!=“All00”)
{
警报('end hour all error')
返回false;
}
else if(end_hour.value==“All00”&start_hour.value!=“All00”)
{
警报(“开始时间所有错误”)
返回false;
}
else if(end_hour.value您不需要将getElementsByName与jQuery一起使用
试试这个
或者,由于您的id似乎与名称相同,因此可以使用此选择器
$('select#start_hour')
您的验证器方法应该这样构造
jQuery.validator.addMethod( "schedule", function(value, element) {
var start_hour = $('select#start_hour');
var end_hour = $('select#end_hour');
alert(start_hour.val());
alert(end_hour.val());
if (start_hour.val() == "All00" && end_hour.val() !="All00") {
alert('end hour all error')
return false;
}
else if (end_hour.val() == "All00" && start_hour.val() !="All00") {
alert('start hour all error')
return false;
}
else if (end_hour.val() <= start_hour.val() ) {
alert('end hour must be larger error')
return false;
}
else return true;
}, "Error with schedule");
jQuery.validator.addMethod(“schedule”,函数(值,元素){
var start_hour=$('select#start_hour');
var end_hour=$('select#end_hour');
警报(start_hour.val());
警报(end_hour.val());
如果(start_hour.val()=“All00”&&end_hour.val()!=“All00”){
警报('end hour all error')
返回false;
}
如果(end_hour.val()=“All00”&&start_hour.val()!=“All00”){
警报(“开始时间所有错误”)
返回false;
}
else if(end_hour.val()您不需要将getElementsByName与jQuery一起使用
试试这个
或者,由于您的id似乎与名称相同,因此可以使用此选择器
$('select#start_hour')
您的验证器方法应该这样构造
jQuery.validator.addMethod( "schedule", function(value, element) {
var start_hour = $('select#start_hour');
var end_hour = $('select#end_hour');
alert(start_hour.val());
alert(end_hour.val());
if (start_hour.val() == "All00" && end_hour.val() !="All00") {
alert('end hour all error')
return false;
}
else if (end_hour.val() == "All00" && start_hour.val() !="All00") {
alert('start hour all error')
return false;
}
else if (end_hour.val() <= start_hour.val() ) {
alert('end hour must be larger error')
return false;
}
else return true;
}, "Error with schedule");
jQuery.validator.addMethod(“schedule”,函数(值,元素){
var start_hour=$('select#start_hour');
var end_hour=$('select#end_hour');
警报(start_hour.val());
警报(end_hour.val());
如果(start_hour.val()=“All00”&&end_hour.val()!=“All00”){
警报('end hour all error')
返回false;
}
如果(end_hour.val()=“All00”&&start_hour.val()!=“All00”){
警报(“开始时间所有错误”)
返回false;
}
else if(end_hour.val()getElementsByName)返回一个数组,即使只有一个结果,也会返回一个数组,因此需要指定需要第一个结果
var start_hour = document.getElementsByName("start_hour")[0];
var end_hour = document.getElementsByName("end_hour")[0];
getElementsByName返回一个数组,即使只有一个结果,因此您需要指定您想要第一个结果
var start_hour = document.getElementsByName("start_hour")[0];
var end_hour = document.getElementsByName("end_hour")[0];
getElementsByName(注意“s”)返回一个数组,因此您必须通过以下方式引用开始值:start\u hour[0]。value
。但是我同意John的观点,您应该明确使用jquery选择器。getElementsByName(注意“s”)返回一个数组,因此您必须通过以下方式引用开始值:start\u hour[0].value
.Bu我同意John的观点,您应该明确使用jquery选择器。您也可以使用jquery的ID选择器“#”
以下方法也有效:
var start_hour = $("#start_hour");
var end_hour = $("#end_hour");
alert(start_hour.val());
alert(end_hour.val());
您还可以使用jQuery的ID选择器“#”
以下方法也有效:
var start_hour = $("#start_hour");
var end_hour = $("#end_hour");
alert(start_hour.val());
alert(end_hour.val());
不,var start_hour=$('input[name=“start_hour”]”);alert(start_hour.value)
似乎仍然返回undefined
查看是否需要使用jQuery选择器。val()代替.value,但如果您已经在使用jQuery,那么最好使用jQuery。当使用jQuery时,您必须意识到返回的是带有选择器的jQuery对象,而不是DOM对象。请稍等,我将修改您的代码以完全使用jQuery.nope,var start_hour=$('input[name=“start_hour”]);提醒(start_hour.value)
似乎仍然返回未定义
查看是否需要使用jQuery选择器而不是.value,但是是的,如果您已经在使用jQuery,那么最好使用jQuery。当使用jQuery时,您必须意识到返回的是带有选择器的jQuery对象,而不是DOM对象。请稍候,我将修改您的代码以完全使用jQuery。