Javascript单选按钮检查无法使用列
我有一个表,其中每个表行有一个表单,每个表单有三个单选按钮,我想获取单选按钮的值并将其存储在数组中。在此函数中从开关盒内部获取警报不起作用:Javascript单选按钮检查无法使用列,javascript,html,dom,Javascript,Html,Dom,我有一个表,其中每个表行有一个表单,每个表单有三个单选按钮,我想获取单选按钮的值并将其存储在数组中。在此函数中从开关盒内部获取警报不起作用: var registerEventToRadio = function registerEventToRadio(formName, radioName, radio) { //alert('aaaa'); works uncommenting this causes 9 alerts... var mandyEvent = function mandy
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;
}
是一个JSFIDLE,它包含所有在那里使用的代码。您的
getRadioButtonArray
返回一个单一的无线电元素,而不是数组,因此您的代码不做任何事情,因为您试图迭代非数组。将for
循环更改为此将使事件运行:
if (radioName === 'mandatory') radio.onclick = mandyEvent;
if (radioName === 'visible') radio.onclick = viEvent;
if (radioName === 'rename') radio.onclick = renoEvent;
仅供参考-将
直接放在
中是无效的HTML。我可以将tr放在表单中吗?不,一个tr
只能放在表
、thead
、tbody
或tfoot
中。我建议您不要依赖于什么都不做的表单,而是通过ID标识相关项。另一方面,即使使用无效的HTML,也可以让您的功能正常工作。使用this.value
而不是switch case
语句中的1。他们会得到单选按钮的值吗?
if (radioName === 'mandatory') radio.onclick = mandyEvent;
if (radioName === 'visible') radio.onclick = viEvent;
if (radioName === 'rename') radio.onclick = renoEvent;