好吧我有一個由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