需要单选按钮 - JavaScript 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6116149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Radio button required - JavaScript validation
提问by vlevsha
I know nothing of JavaScript. I had to add a group of two radio buttons to an HTML form with values "yes" and "no". Now I need to make them "required" There are several other required fields in the form and this piece of JavaScript:
我对 JavaScript 一无所知。我不得不将一组两个单选按钮添加到一个 HTML 表单中,其值为“yes”和“no”。现在我需要让它们成为“必需的”表单和这段 JavaScript 中还有其他几个必填字段:
<SCRIPT LANGUAGE="JavaScript">
<!--
reqd_fields = new Array();
reqd_fields[0] = "name";
reqd_fields[1] = "title";
reqd_fields[2] = "company";
reqd_fields[3] = "address";
reqd_fields[4] = "city";
reqd_fields[5] = "state";
reqd_fields[6] = "zip";
reqd_fields[7] = "phone";
reqd_fields[8] = "email";
reqd_fields[9] = "employee";
function validate(form_obj) {
if (test_required && !test_required(form_obj)) {
return false;
}
It was done by someone else, not me. What I did is just added my field to this array, like this:
这是别人做的,不是我做的。我所做的只是将我的字段添加到这个数组中,如下所示:
reqd_fields[10] = "acknowledge";
However it doesn't seem to be working.
但是,它似乎不起作用。
Please guide me as I am totally ignorant when it comes to JavaScript.
请指导我,因为我对 JavaScript 一无所知。
回答by locrizak
Why don't you just make one selected by default then one will always be selected.
为什么不默认选择一个,然后将始终选择一个。
回答by jszpila
A link to your page or a sample of your HTML would make this easier, but I'm going to hazard a guess and say that the values in the array match the "name" attribute of your radio button elements.
链接到您的页面或您的 HTML 示例将使这更容易,但我将冒险猜测并说数组中的值与您的单选按钮元素的“名称”属性匹配。
If this the case, "acknowledge" should be the name of bothradio buttons, and to make things easier, one should have the attribute "checked" set to "true" so there is a default, so you'll get a value either way.
如果是这种情况,“acknowledge”应该是两个单选按钮的名称,为了使事情更容易,应该将属性“checked”设置为“true”,这样就有了一个默认值,所以你会得到一个值道路。
So, something like this:
所以,像这样:
<input type="radio" name="acknowledge" value="yes" /> Yes <br/>
<input type="radio" name="acknowledge" value="no" checked="true" /> No <br/>
回答by maerics
Without seeing your HTML and more context of your validate function it's unclear exactly what you're looking for, but here's an example of how to require a selected value from a radio group:
没有看到您的 HTML 和验证功能的更多上下文,就不清楚您正在寻找什么,但这里有一个示例,说明如何要求从单选组中选择一个值:
<form name="form1">
<input type="radio" name="foo"> Foo1<br/>
<input type="radio" name="foo"> Foo2<br/>
</form>
<script type="text/javascript">
var oneFooIsSelected = function() {
var radios = document.form1.foo, i;
for (i=0; i<radios.length; i++) {
if (radios[i].checked) {
return true;
}
return false;
};
</script>
Here is a working example on jsFiddle.
这是一个关于 jsFiddle的工作示例。
回答by Lloyd Medley
I know question is ancient but this is a simple solution that works.
我知道问题很古老,但这是一个有效的简单解决方案。
<script type="text/javascript">
function checkForm(formname)
{
if(formname.radiobuttonname.value == '') {
alert("Error: Please select a radio button!");
return false;
}
document.getElementById('submit').value='Please wait..';void(0);
return true;
}
</script>
<form name="formname" onsubmit="return checkForm(this)"
<input type="radio" value="radio1" name="radiobuttonname" style="display:inline;"> Radio 1<br>
<input type="radio" value="radio2" name="radiobuttonname" style="display:inline;"> Radio 2<br>
<input type="submit" value="Submit">
</form>
回答by mcgrailm
I always recommend using jQuery validateseems better to me than trying to re-invent the wheel
我总是建议使用jQuery validate对我来说比尝试重新发明轮子更好