我的JS程序有一些奇怪的问题。我有这个工作正常,但由于某种原因,它不再工作。我只是想找到单选按钮的值(其中一个被选中)并将其返回给一个变量。由于某种原因,它总是返回undefined。

这是我的代码:

function findSelection(field) {
    var test = 'document.theForm.' + field;
    var sizes = test;

    alert(sizes);
        for (i=0; i < sizes.length; i++) {
            if (sizes[i].checked==true) {
            alert(sizes[i].value + ' you got a value');     
            return sizes[i].value;
        }
    }
}

submitForm:

function submitForm() {

    var genderS =  findSelection("genderS");
    alert(genderS);
}

HTML:

<form action="#n" name="theForm">

    <label for="gender">Gender: </label>
    <input type="radio" name="genderS" value="1" checked> Male
    <input type="radio" name="genderS" value="0" > Female<br><br>
    <a href="javascript: submitForm()">Search</A>
</form>

当前回答

我喜欢使用括号从输入中获取值,它比使用圆点更清楚。

document.forms['form_name']['input_name'].value;

其他回答

试试这个

function findSelection(field) {
    var test = document.getElementsByName(field);
    var sizes = test.length;
    alert(sizes);
    for (i=0; i < sizes; i++) {
            if (test[i].checked==true) {
            alert(test[i].value + ' you got a value');     
            return test[i].value;
        }
    }
}


function submitForm() {

    var genderS =  findSelection("genderS");
    alert(genderS);
    return false;
}

这里有一把小提琴。

你可以这样做:

var收音机= document.getElementsByName('性别'); 对于(var I = 0,长度= radio .length;I < length;我+ +){ If(收音机[i].checked) { //做任何你想做的检查收音机 警报(无线电[我]。value); //逻辑上只能检查一个无线电,不要检查其他的 打破; } } <label for="gender">性别:</label> <input type="radio" name=" lads " value="1" checked="checked">Male</input> . <input type="radio" name="gender " value="0">Female</input> .

斯菲德尔

编辑:感谢HATCHA和jsetung的编辑建议。

首先,大声喊出ashraf aaref,他的回答我想扩展一点。

正如MDN Web文档建议的那样,使用RadioNodeList是首选的方法:

// Get the form
const form = document.forms[0];

// Get the form's radio buttons
const radios = form.elements['color'];

// You can also easily get the selected value
console.log(radios.value);

// Set the "red" option as the value, i.e. select it
radios.value = 'red';

不过,也可以通过querySelector选择表单,这也很好:

const form = document.querySelector('form[name="somename"]')

但是,直接选择无线电将不起作用,因为它将返回一个简单的NodeList。

document.querySelectorAll('input[name="color"]')
// Returns: NodeList [ input, input ]

在选择表单时,首先返回一个RadioNodeList

document.forms[0].elements['color']
// document.forms[0].color # Shortcut variant
// document.forms[0].elements['complex[naming]'] # Note: shortcuts do not work well with complex field names, thus `elements` for a more programmatic aproach
// Returns: RadioNodeList { 0: input, 1: input, value: "red", length: 2 }

这就是为什么必须首先选择表单,然后调用元素方法。除了所有输入节点之外,RadioNodeList还包括一个属性值,它支持这种简单的操作。

参考:https://developer.mozilla.org/en-US/docs/Web/API/RadioNodeList/value

把Ed Gibbs的答案代入一个一般函数:

function findSelection(rad_name) {
    const rad_val = document.querySelector('input[name=' + rad_name + ']:checked');
    return (rad_val ? rad_val.value : "");
}

然后你可以执行findSelection("性别");

我喜欢使用括号从输入中获取值,它比使用圆点更清楚。

document.forms['form_name']['input_name'].value;