2014-01-27 78 views
0

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

回答

0

您没有使用选择ID使用选择ID

var sel=document.getElementById('Forex').value; 
    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"; 
} 
+0

对不起@Srinu MI不明白你的答案,你可以打破它,我以前使用的是 – Tomazi

+0

这样VAR SEL =的document.getElementById ( 'FX')值= “外汇”;所以你使用的选项ID使用选择ID,因为值将只存储在选择ID,所以使用这个var sel = document.getElementById('Forex')。值;和其余的代码是相同的 –

+0

嗨那里如果我使用你的答案似乎工作,但如果我添加另一个下拉菜单的东西变得混乱,请检查我编辑的问题 – Tomazi

0

我做的javaScript下拉验证。当在下拉值“中选择一个”在那段时间给你的消息“请选择你的价值”,在github上。项目链接。我的孔项目 是Javascript drop-down value validation

+0

如果你想我可以在这里添加代码 –

0

,你必须使用下拉ID“外汇”用于获取下拉选项没有ID的选择的值,如下面

var sel = document.getElementById('Forex').value; 
0

HTML代码

<form id="send" name="myfrom" action="confrimationPage.php" method="POST" onsubmit="return validateForm(this);" > 

       <p> 
        <label for="company">Gender</label> 
        <select id="gender" name="gender"> 
         <option value="Select One">Select One</option> 
         <option>Male</option> 
         <option>Female</option> 
        </select> 
       </p> 

       <p> 
        <label for="country">Postal Code</label> 
        <input id="Country" type="text" name="postalCode" /> 
       </p> 

       <p> 
        <button id="submit" type="submit">Submit</button> 
       </p> 

      </form> 

* 的JavaScript *

<script type="text/javascript"> 

function validateForm(form) 
{ 
    // Gender empty or not 
    var selectGender=document.getElementById('gender').value; 
    if(selectGender=="Select One") 
    { 
     alert("Please Select gender"); 
     return false; 
    } 
    //Postal code empty or not 
    if(form.postalCode.value=="") 
    { 
     alert("Filled the Postal Code Field"); 
     form.postalCode.focus(); 
     return false; 
    } 
} 

</script> 
0
Check It 


var sel=document.getElementById('Forex').value; 
var binary=document.getElementById('Binary').value; 



if(sel.match("Forex")){ 
     document.getElementById('error').innerHTML = "Choose Forex Workshop Date"; 
     document.getElementById('Forex').style.borderColor = "red"; 
     return false; 
}else{ 
    document.getElementById('error').innerHTML = ""; 
    document.getElementById('Forex').style.borderColor = "green"; 
} 
if(binary.match("Binary")){ 
     document.getElementById('error').innerHTML = "Choose Forex Workshop Date"; 
     document.getElementById('Binary').style.borderColor = "red"; 
     return false; 
}else{ 
    document.getElementById('error').innerHTML = ""; 
    document.getElementById('Binary').style.borderColor = "green"; 
} 
+0

谢谢你尝试这也不工作不让我提交...即使所有的条件都满足,当我点击提交按钮什么也没有发生 – Tomazi

+0

您是使用 use <输入类型=“提交”> –

+0

是的,它提交,我现在正在做PHP后端验证,但我需要前端验证以及那我不是很擅长,所以我不知道我要去哪里错了它代码看起来不错,对我来说很有意义:/ – Tomazi