2013-09-30 30 views
0

我想要使用JavaScript/HTML驗證一些單選按鈕,但是我遇到了問題。這件作品的腳本我有麻煩如下 -單選按鈕有效期

 if ((RegistrationForm.sexm[0].checked == false) && (RegistrationForm.sexf[1].checked == false)) 
     { 
      alert("Please select your sex"); 
      return false; 
     } 

基本上我想如果選擇了沒有單選按鈕選擇它返回一個警告。在提交表格的時候,自己會提醒自己,而不是提供任何提醒。任何意見,將不勝感激!

我一直在提供工作相關的HTML是 -

<form id="RegistrationForm" onsubmit="ValidateForm()" action=""> 
<fieldset> 
    <legend>Registration Form</legend> 
    <label for="username">User Name: </label> 
    <input type="text" class="input" id="username"/><br /> 
    <label for="password">Password: </label> 
    <input type="password" class="input" id="password"/><br /> 
    <label for="repassword">Re-Type Password: </label> 
    <input type="password" class="input" id="repassword"/><br /> 
    <label for="email">Email Address: </label> 
    <input type="text" class="input" size="30" id="email"/><br /> 
    <label>Age: </label> 
    <select id ="age" name="age"> 
     <option value=""> --- </option> 
     <option value="0-18">0-18</option> 
     <option value="18-30">18-30</option> 
     <option value="30-50">30-50</option> 
     <option value="50+">50+</option> 
    </select><br/> 
    <label for="sexm">Sex:</label> 
    <input type="radio" id="sexm" name="sex" value="male" />Male<br/> 
    <label for="sexf">&nbsp; </label> 
    <input type="radio" id="sexf" name="sex" value="female" />Female<br/> 
    <input type="checkbox" name="agree" id="agree" value="agree"/> 
    <label for="agree">I accept the <a href="">terms and cond</a>.</label> <br/> 
    <label>&nbsp; </label> 
    <input type="submit" value="Submit" class="button" /> 
</fieldset> 
</form> 
+0

您正在使用''&&如果中。所以只有在兩個都沒有選中時纔會進入IF。你是否嘗試過不選擇他們的任何一個。 –

+0

你可以看看:http://stackoverflow.com/questions/18262432/radio-buttons-post-show-value-even-none-of-the-buttons-are-checked-how-to-valida –

+0

是的,這是我以後,我只想要一個警報,如果既沒有選擇框。但是,即使將它們都留空,我也不會收到警告消息。對不起,如果我誤解了你的意思! –

回答

2

在JavaScript試試這個代碼<script>標籤

function ValidateForm() { 
    if ((document.getElementById("sexm").checked == false) && (document.getElementById("sexm").checked == false)) 
    { 
     alert("Please select your sex"); 
     return false; 
    } else { 
     return true; 
    } 
} 

將其替換你的代碼是

if ((RegistrationForm.sexm[0].checked == false) && (RegistrationForm.sexf[1].checked == false)) 
{ 
    alert("Please select your sex"); 
    return false; 
} 
1

使用for循環來檢查用戶是否選擇了任何價值或沒有,如果沒有,不是拋出一個警告,並使用return false來防止您的表單被提交。

Demo

var ischecked = null; 
var radios = document.getElementsByName('sex'); 
for (var i = 0; i < radios.length; i++) { 
      if (radios[i].checked) { 
      ischecked = radios[i]; 
    } 
} 
if(ischecked==null) { 
    alert('Please choose your Sex'); 
    return false; 
} 

保存在功能上面的代碼,並把它提交您的形式。


Demo 2(生效上提交)

注意:您需要使用onsubmit="return ValidateForm();"

0

試試這個...

<script type="text/javascript"> 
function validate() 
{ 
var o = document.getElementById('sexm'); 
var t = document.getElementById('sexf'); 

if ((o.checked == false) && (t.checked == false)) 
{ 
alert ("please select your sex"); 

return false; 
} 
return true; 
} 
</script>