需要单选按钮 - JavaScript验证 [英] Radio button required - JavaScript validation

查看:55
本文介绍了需要单选按钮 - JavaScript验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript一无所知。
我必须在HTML表单中添加一组两个单选按钮,其值为是和否。
现在我需要让它们必需
表单和JavaScript中还有其他几个必填字段:

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:

    <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.

请指导我,因为我对JavaScript完全无知。

Please guide me as I am totally ignorant when it comes to JavaScript.

推荐答案

指向您网页的链接或你的HTML样本会让这更容易,但我会冒险猜测并说数组中的值与单选按钮元素的name属性相匹配。

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.

如果是这种情况,确认应该是两个单选按钮的名称,为了方便起见,应该有属性checked设置为true,因此有一个默认值,所以你将获得一个值。

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.

所以,像这样:

<input type="radio" name="acknowledge" value="yes" /> Yes <br/>
<input type="radio" name="acknowledge" value="no" checked="true" /> No <br/>

这篇关于需要单选按钮 - JavaScript验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆