简体   繁体   English

需要单选按钮 - JavaScript 验证

[英]Radio button required - JavaScript validation

I know nothing of JavaScript.我对 JavaScript 一无所知。 I had to add a group of two radio buttons to an HTML form with values "yes" and "no".我必须将一组两个单选按钮添加到 HTML 表单,其值为“yes”和“no”。 Now I need to make them "required" There are several other required fields in the form and this piece of JavaScript:现在我需要将它们设为“必填” 表单中还有其他几个必填字段,以及这一段 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 一无所知。

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 both radio 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/>

Why don't you just make one selected by default then one will always be selected.您为什么不默认选择一个,然后将始终选择一个。

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>

I always recommend using jQuery validate seems better to me than trying to re-invent the wheel我总是推荐使用jQuery 验证对我来说似乎比尝试重新发明轮子更好

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 和您的验证 function 的更多上下文的情况下,不清楚您正在寻找什么,但这里有一个示例,说明如何要求从无线电组中选择一个值:

<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 上的一个工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM