使用javascript的总菜鸟。我正在尝试更改功能。这是目前存在并且有效的。
function hideStateField(theForm) {
theForm.state.disabled = true;
theForm.state.className = 'hiddenField';
theForm.state.setAttribute('className', 'hiddenField');
document.getElementById("stateLabel").className = 'hiddenField';
document.getElementById("stateLabel").setAttribute('className', 'hiddenField');
document.getElementById("stateText").className = 'hiddenField';
document.getElementById("stateText").setAttribute('className', 'hiddenField');
document.getElementById("stateBreak").className = 'hiddenField';
document.getElementById("stateBreak").setAttribute('className', 'hiddenField');
}
我想让它更通用,因此它不特定于“状态”字段。所以我正在更改函数名称以反映这一点并添加第二个参数。然后我尝试使用第二个参数作为变量来代替我们看到的“状态”。
function hideAddressField(theForm,theField) {
theForm.theField.disabled = true;
theForm.theField.className = 'hiddenField';
theForm.theField.setAttribute('className', 'hiddenField');
document.getElementById(theField+"Label").className = 'hiddenField';
document.getElementById(theField+"Label").setAttribute('className', 'hiddenField');
document.getElementById(theField+"Text").className = 'hiddenField';
document.getElementById(theField+"Text").setAttribute('className', 'hiddenField');
document.getElementById(theField+"Break").className = 'hiddenField';
document.getElementById(theField+"Break").setAttribute('className', 'hiddenField');
}
我只是用“状态”作为第二个变量对其进行了测试,以确保它有效......但它没有。我不断收到“未捕获的类型错误:无法设置未定义的属性‘禁用’”。我确定它是一个语法错误。我对这个函数的调用是:
hideAddressField(theForm,state);
表单的名称也是“theForm”,所以我认为变量“theForm”被分配了“theForm”的值,而变量“theField”被分配了“state”的值,这两个函数应该是等价的。明显不是。
我哪里错了?