[英]Drop Down Menu Validation JS
I have a dynamically generated drop down menus using PHP, I use these manes on different web pages, Now I also want to add some JS validation to these menus so that the user does not forget to choose an option while filling in a form. 我有一个使用PHP动态生成的下拉菜单,我在不同的网页上使用了这些菜单,现在我还想向这些菜单添加一些JS验证,以便用户在填写表单时不会忘记选择一个选项。
The problem is that my JS code does not work and does not validate my menus, The code looks good to me I have IF statements where they check if the first option value =0 therefore there has not been selection but it still does not work 问题是我的JS代码不起作用并且无法验证我的菜单,代码对我来说看起来不错,我有IF语句,它们在其中检查第一个选项值是否为= 0,因此没有选择,但仍然无法正常工作
PHP code: PHP代码:
$option='<select id="Forex" name="workshop">';
$option.='<option value="0">Select Forex Workshop</option>';
$option.='';
while($result = mysqli_fetch_assoc($query))
{
if($timestamp < $result['endingDate'])
{
$option.='<option id="'.$result['id'].'" value='.$result['endingDate'].'>'.$result['course']." ".$result['schedule'].'</option>';
}
}
$option.='</select>';
return $option;
}
function getBinary($link){
$timestamp = date("Y-m-d");
$query2 = mysqli_query($link, $sql2);
$option2='<select id="Binary" name="workshop">';
$option2.='<option value="0">Select Binary Workshop</option>';
$option2.='';
while($result2 = mysqli_fetch_assoc($query2))
{
if($timestamp < $result2['endingDate'])
{
$option2.='<option id="'.$result2['id'].'" value="'.$result2['endingDate'].'">'.$result2['course']." ".$result2['schedule'].'</option>';
}
}
$option2.='</select>';
return $option2;
}
?>
JS CODE: JS代码:
var workshop=document.getElementById('Binary').value;
if(workshop==0){
document.getElementById('Binary').style.borderColor = "red";
document.getElementById('error').innerHTML = "Please Select Workshop1";
return false;
}else{
document.getElementById('Binary').style.borderColor = "green";
}
var workshop2=document.getElementById('Forex').value;
if(workshop2==0){
document.getElementById('Forex').style.borderColor = "red";
document.getElementById('error').innerHTML = "Please Select Workshop";
return false;
}else{
document.getElementById('Forex').style.borderColor = "green";
}
You have wrongly named the select id/name. 您错误地命名了选择ID /名称。
$option='<select id="Forex" name="workshop">';
and yet you are trying to access it from JS by calling it by id workshop while the name attribute
is workshop not the id. 但是,您正在尝试通过id Workshop调用它来从JS访问它,而
name attribute
不是Workshop而是id。
var work=document.getElementById('workshop').value;
The id is Forex not workshop. ID是外汇,不是工作坊。 And you call Forex from different Js line.
然后您从不同的Js行调用外汇。 I think your error is in trying to access a null variable.
我认为您的错误在于尝试访问null变量。 Unless you have another element with the id workshop that I'm not aware of.
除非您还有我不知道的id工作坊的另一个要素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.