Javascript单选按钮检查使用列不起作用

时间:2014-01-18 13:11:27

标签: javascript html dom

我有一张桌子,每张桌子都有一个表格,每个表格有三个单选按钮,我想得到radioButton的值并将其存储在一个数组中。在此函数中从开关盒内部获取警报不工作:

 var registerEventToRadio = function registerEventToRadio(formName, radioName, radio) {
//alert('aaaa'); works uncommenting this causes 9 alerts...
var mandyEvent = function mandyEvent(event) {
    switch (formName) {
        case 'rowform_chandelier':
            mandatoryArr[0] = 1;
            alert(1); //does not work
            break;
        case 'rowform_fireplace':
            mandatoryArr[1] = 1;
            alert(1); //does not work
            break;
        case 'rowform_rooflights':
            mandatoryArr[2] = 1;
            alert(1); //does not work
            break;
    }
};

var viEvent = function viEvent(event) {
    switch (formName) {
        case 'rowform_chandelier':
            visibleArr[0] = 1;
            alert(1); //does not work
            break;
        case 'rowform_fireplace':
            visibleArr[1] = 1;
            alert(1); //does not work
            break;
        case 'rowform_rooflights':
            visibleArr[2] = 1;
            alert(1); //does not work
            break;
    }
};

var renoEvent = function renEvent(event) {
    switch (formName) {
        case 'rowform_chandelier':
            renameArr[0] = 1;
            alert(1); //does not work
            break;
        case 'rowform_fireplace':
            renameArr[1] = 1;
            alert(1); //does not work
            break;
        case 'rowform_rooflights':
            renameArr[2] = 1;
            alert(1); //does not work
            break;
    }
};
var i = 0;
for (i = 0; i < radio.length; i++) {
    if (radioName === 'mandatory') radio[i].onclick = mandyEvent;
    if (radioName === 'visible') radio[i].onclick = viEvent;
    if (radioName === 'rename') radio[i].onclick = renoEvent;
}

};

以这种方式调用此函数:

var reno = getRadioButtonArray(formName, 'rename');
registerEventToRadio(formName, 'mandatory', mandy);
registerEventToRadio(formName, 'visible', vi);
registerEventToRadio(formName, 'rename', reno);

This是一个JSFiddle,它包含了那里使用的所有代码。

1 个答案:

答案 0 :(得分:1)

您的getRadioButtonArray会返回单个无线电元素,而不是数组,因此您的代码不会执行任何操作,因为您尝试迭代非数组。将for循环更改为此将使事件运行:

if (radioName === 'mandatory') radio.onclick = mandyEvent;
if (radioName === 'visible') radio.onclick = viEvent;
if (radioName === 'rename') radio.onclick = renoEvent;

http://jsfiddle.net/2wMCZ/15/

仅供参考 - 直接在<form>内放置<tr>无效HTML。