0
我有一個表格,我有一個表格,每個表格有三個單選按鈕,我想獲得radioButton的值並將其存儲在一個數組中。獲取警報從此功能開關外殼內不起作用:Javascript單選按鈕檢查不工作使用列
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它由那裏使用的所有代碼。
我可以在表單中放置一個tr嗎? – vamsiampolu
不,tr'只能放在'table','thead','tbody'或'tfoot'裏面。我建議,不要依賴不做任何事情的表單,而是通過ID來識別相關項目。另一方面,即使使用無效的HTML,也可以使您的功能正常工作。 – JLRishe
在'switch-case'語句中使用'this.value'而不是1,他們會得到單選按鈕的值嗎? – vamsiampolu