2013-01-07 22 views
-1

我想驗證JavaScript中的單選按鈕和選項框。下面是代碼,但它不工作... JavaScript代碼OprionBox和收音機:JavaScript中的表單驗證(廣播和組合框)

function checkCountry() 
     { 
     if(document.form.country.selectedIndex=="") 
     { 
     alert("Please select country from the list"); 
     return false; 
     } 
     return true 
     } 
function checkGender() 
     { 
     if(!document.getElementsByName("sex")[0].checked && !document.getElementsByName("sex")[1].checked) 
     { 
     alert("Select Male/Female"); 
     return false; 
     } 
     return true; 
     } 
function validate() 
     { 
      checkCountry(); 
      checkGender(); 
     } 

HTML代碼:

<form name="form" method="post" onSubmit="return validate()">     
    <select name="country"> 
    <option value="select">(Please select a country)</option> 
    <option value="pk">Pakistan</option> 
    <option value="chn">China</option> 
    <option value="uk">United Kingdom</option> 
    <option value="usa">United States of America</option> 
    <option value="ir">Iran</option> 
    <option value="ma">Malaysia</option> 
    </select><br> 
    <input type="radio" name="sex" value="male">Male<br> 
    <input type="radio" name="sex" value="female">Female<br></form> 

請幫助...

+0

你什麼都沒有說。 「它不工作」和「請幫助」不是一個問題。請嘗試形成更詳細的問題。 – Chris

+0

我真正需要的是,當我將這些表單組件留空時,它應該顯示警報錯誤消息......但事實並非如此。 – user1880157

+0

在嘗試進行驗證之前,請嘗試理解HTML的文檔模型。 – Cris

回答

0

你必須將您檢查所選索引的行更改爲:

if(document.form.country.selectedIndex === 0)那是因爲您的「請選擇一個國家/地區」條目,您將始終有一個選擇特德指數。

如果我然後在您的表單中添加<input type="submit" value="test"/>以測試提交,我會收到您的錯誤消息。

請參閱http://jsbin.com/oviped/2/edit它正在工作!

+0

這又是一樣的......當我不選擇任何國家時,不會給出錯誤信息。 – user1880157

+0

我編輯了我的答案,請參閱鏈接。我試了一下,它確實有效。 – Chris

0

變化if(document.form.country.selectedIndex=="")if(document.getElementsByName("country").selectedIndex=="0")

+0

我複製了你的行,但沒有再次顯示結果。 – user1880157

0

這是整個代碼和它的工作的罰款正在發生的事情錯在這裏

<html> 
<head> 
<script type="text/javascript"> 
function checkGender() 
     { 
     if(!document.getElementsByName("sex")[0].checked && !document.getElementsByName("sex")[1].checked) 
     { 
     alert("Select Male/Female"); 
     return false; 
     } 
     return true; 
     } 
function checkCountry() 
     { 

     if(document.getElementsByName("country")[0].selectedIndex==0) 
     { 
     alert("Please select country from the list"); 
     return false; 
     } 
     return true 
     } 
function validate() 
     { 
      checkCountry(); 
      checkGender(); 
     } 
</script> 
</head> 
<body> 

<form onSubmit="return validate()" > 
    <select name="country"> 
    <option value="select">(Please select a country)</option> 
    <option value="pk">Pakistan</option> 
    <option value="chn">China</option> 
    <option value="uk">United Kingdom</option> 
    <option value="usa">United States of America</option> 
    <option value="ir">Iran</option> 
    <option value="ma">Malaysia</option> 
    </select><br> 
    <input type="radio" name="sex" value="male">Male<br> 
    <input type="radio" name="sex" value="female">Female<br> 
    <input type="submit" value="Submit" > 
</form> 

</body> 
</html>