Let's pretend you have HTML like this
(假设您有这样的HTML)
<input type="radio" name="gender" id="gender_Male" value="Male" />
<input type="radio" name="gender" id="gender_Female" value="Female" />
For client-side validation, here's some Javascript to check which one is selected:
(对于客户端验证,以下是一些Java脚本来检查选择了哪一个:)
if(document.getElementById('gender_Male').checked) {
//Male radio button is checked
}else if(document.getElementById('gender_Female').checked) {
//Female radio button is checked
}
The above could be made more efficient depending on the exact nature of your markup but that should be enough to get you started.
(根据标记的确切性质,可以使上述操作更为有效,但这足以使您入门。)
If you're just looking to see if any radio button is selected anywhere on the page, PrototypeJS makes it very easy.
(如果您只是想查看页面上的任何位置是否选中了任何单选按钮,则PrototypeJS使其非常容易。)
Here's a function that will return true if at least one radio button is selected somewhere on the page.
(如果在页面的某处选择了至少一个单选按钮,则此函数将返回true。)
Again, this might need to be tweaked depending on your specific HTML. (同样,这可能需要根据您的特定HTML进行调整。)
function atLeastOneRadio() {
return ($('input[type=radio]:checked').size() > 0);
}
For server-side validation (remember, you can't depend entirely on Javascript for validation!) , it would depend on your language of choice, but you'd but checking the gender
value of the request string.
(对于服务器端验证(请记住,您不能完全依靠Java进行验证!) ,这取决于您选择的语言,但是您只需要检查请求字符串的gender
值即可。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…