好吧我有一个由PHP动态生成的下拉菜单,并填充从数据库中获取的数据。在这方面,它的工作很好,我遇到的问题是使用JS验证,JavaScript不太好('不喜欢它'),但为了我的迷你项目的目的,我必须使用它。爪哇脚本下拉菜单验证
问题是,我用JS检查用户是否选择了下拉菜单中可用的选项之一,因此可以提交表单,否则显示警告。
所以我想要做的是这个.....给第一个选项显示“PLEASE SELECT workshop”的值为“f”,所以如果在提交表单时这个下拉的值菜单== f返回false否则提交表单。
现在,如果在提交值== f错误显示,但如果该值不是== f,我从下拉菜单中选择一个值我不能继续提交它不允许我
PHP代码:
function getForex($link){
$timestamp = date("Y-m-d");
$sql = "SELECT id, course, status, endingDate, schedule FROM courses WHERE course LIKE '%Forex%' OR course LIKE '%forex%' AND status=5";
$query = mysqli_query($link, $sql);
$option='<select id="Forex" name="workshop">';
$option.='<option id="fx" value="Forex">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;
}
JS代码:许多非常有用的答案的
var sel=document.getElementById('fx').value="Forex";
if(sel.match("Forex")){
document.getElementById('error').innerHTML = "Choose Forex Workshop Date";
document.getElementById('fx').style.borderColor = "red";
return false;
}else{
document.getElementById('fx').style.borderColor = "green";
}
OK家伙组合帮助,但是当我添加另一个下拉菜单一样
PHP代码:
function getBinary($link){
$timestamp = date("Y-m-d");
$sql2 = "SELECT id, course, status, endingDate, schedule FROM courses WHERE course LIKE '%Binary%' OR course LIKE '%binary%' AND status=5";
$query2 = mysqli_query($link, $sql2);
$option2='<select id="Binary" name="workshop">';
$option2.='<option id="bi" value="Binary">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代码:
var selbi=document.getElementById('Binary').value;
if(selbi.match("Binary")){
document.getElementById('error').innerHTML = "Choose Binary Workshop Date";
return false;
}
的问题变成内在HTML消息只显示为外汇,如果没有选择外汇避开即扭曲它显示的信息选择外汇车间,如果我选择外汇车间,然后显示选择二元车间:D
对不起@Srinu MI不明白你的答案,你可以打破它,我以前使用的是 – Tomazi
这样VAR SEL =的document.getElementById ( 'FX')值= “外汇”;所以你使用的选项ID使用选择ID,因为值将只存储在选择ID,所以使用这个var sel = document.getElementById('Forex')。值;和其余的代码是相同的 –
嗨那里如果我使用你的答案似乎工作,但如果我添加另一个下拉菜单的东西变得混乱,请检查我编辑的问题 – Tomazi