【发布时间】:2014-01-27 11:30:10
【问题描述】:
好的,我有一个由 PHP 动态生成并填充了从数据库中获取的数据的下拉菜单。从正面看,这很好用,我遇到的问题是 JS 验证,对 JavaScript 不太好(“不喜欢它”),但出于我的迷你项目的目的,无论如何我都必须使用它......
问题在于,使用 JS,我正在检查用户是否选择了下拉菜单中可用的选项之一,以便可以提交表单,否则会显示警告。
所以我想要做的是......给第一个选项设置一个 value="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";
}
好吧,伙计们结合了许多非常有用的答案,但是当我添加另一个下拉菜单时,例如
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
【问题讨论】:
标签: javascript php