问题描述
我有一个注册表,其中有一个显示男性或女性的性别广播选项.在HTML中没有被选中,因为我希望用户选中一个.像这样:
I have a registration form wich has a gender radio option showing male or female. in the HTML none are checked because i want the user to check one. like this:
<label class="radio">
<input type="radio" name="gender" id="optionsRadios1" value="female">Female
</label>
<label class="radio">
<input type="radio" name="gender" id="optionsRadios2" value="male">Male
</label>
我通过jquery ajax提交数据.奇怪的是,al输入的帖子显示了
I submit data trough jquery ajax. The strange thing is that the post of al inputs shows
[性别] =>男性(如果您未选中两个选项的情况下提交表格)
[gender] => male if you submit the form with none of the 2 options checked
那么,如果它从不发空,该如何验证呢?
So how can i validate this if it never posts empty?
*如果未选择任何选项,我想返回一个错误,以强制用户选择正确的性别.
*I want to return a error if no option is selected so it forces a user to select a right gender..
推荐答案
一种非常快捷的方法是默认在UI中进行选择.这样可以确保该值是非空的,因为您不能取消选中"单选按钮.
One really quick way of doing this is defaulting to a choice in the UI. This makes sure that the value is non-empty because you can't "uncheck" a radio button.
<input type="radio" name="gender" id="optionsRadios1" value="female" checked> Female
编写您自己的提交函数,并将其绑定到提交按钮的onclick()
Write your own submit function, and bind it to your submit button's onclick()
function submit() {
if (!$("#optionsRadios1").prop("checked") && !$("#optionsRadios2").prop("checked")) {
alert("please select a gender");
}
else {
//Whatever you do to submit
}
}
这篇关于单选按钮发布显示值,甚至没有按钮被检查如何验证这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!