我想要使用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"> </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> </label>
<input type="submit" value="Submit" class="button" />
</fieldset>
</form>
您正在使用''&&如果中。所以只有在兩個都沒有選中時纔會進入IF。你是否嘗試過不選擇他們的任何一個。 –
你可以看看:http://stackoverflow.com/questions/18262432/radio-buttons-post-show-value-even-none-of-the-buttons-are-checked-how-to-valida –
是的,這是我以後,我只想要一個警報,如果既沒有選擇框。但是,即使將它們都留空,我也不會收到警告消息。對不起,如果我誤解了你的意思! –