select 및 다른 컨트롤이있는 HTML 페이지가 있습니다. 두 번째 컨트롤의 형식을 첫 번째 컨트롤에서 선택한 값에 따라 동적으로 변경해야합니다.IE에서 자바 스크립트 제거/addChild 문제
이<form name="myForm">
<select name="tagName"
onChange="changeControl(document.myForm.customControl, document.myForm.tagName.value, getSelectedText(document.myForm.tagName));">
<option value='F'>Create Input</option>
<option value='E'>Create Select</option>
<option value='M'>Create Multiple Select</option>
<option value='N'>Create Not Editable</option>
<option value='C'>Create Configuration Param</option>
<option value='D'>Create Date</option>
<option value='X'>Create Unknown</option>
</select>
<br>
<br>
<div>
<input type="hidden" name="customControl" />
</div>
자바 스크립트 함수는 기본적으로 기존 컨트롤을 제거하고 새로운 하나를 추가합니다 다음은 HTML 코드입니다. 현재 Firefox에서는 작동하지만 Internet Explorer에서는 작동하지 않습니다. 여기에 코드가 있습니다.
function changeControl(oldControl, valType, tagName)
{
var newControl;
var controlParent = oldControl.parentNode;
var controlName = oldControl.name;
controlParent.removeChild (oldControl);
switch(valType) {
//IE does not allow name attr to be set later,
//so we must use name at creation time;
//this way of doing things breaks non-IE browsers
//TBD: surround it with try-catch block
case 'F':
//newControl = document.createElement('input'); //non-IE
newControl = document.createElement('input name=' + controlName);
newControl.size = 30;
break;
case 'E':
case 'M':
newControl = document.createElement('select name=' + controlName);
if (valType == 'M')
newControl.multiple = "multiple";
var optionStr = sampleArr[tagName];
optionArr = optionStr.split(';');
var optCount = 0;
for (i in optionArr) {
option = optionArr[i];
newControl.options[i] = new Option(option, option);
}
break;
case 'N':
...
default:
//unrecognized type
newControl = document.createElement('input name=' + controlName);
newControl.value = "Unrecognized control type";
newControl.size = 30;
newControl.style.border = "0px";
newControl.disabled = "disabled";
//newControl.readonly = "readonly";
}
//newControl.name = controlName; //non-IE
controlParent.appendChild (newControl);
}
function getSelectedText(selectObj)
{
return selectObj.options[selectObj.selectedIndex].text;
}
changeControl에 대한 두 번째 호출에서 oldControl은 모든 특성을 잃은 것으로 보입니다. IE에서 name 속성을 동적으로 설정하려고하는 마지막 변경 사항을 확인하십시오.
왜이 부분이 고장 났는지 알 수 있습니까?
name 특성의 MSDN 문서에서 'Microsoft JScript는 런타임에 이름을 변경할 수 있습니다. 이로 인해 프로그래밍 모델의 이름은 요소 컬렉션에서 변경되지 않지만 요소 제출에 사용되는 이름은 변경됩니다. IE에서 양식 요소의 이름을 동적으로 설정할 수있는 것처럼 보입니다. createElement와 함께 사용되는 경우도 있습니다. –