我的代码的设计方式是,一个字段中的输入将确定是否启用下一个字段,以至于如果第一个输入什么都没有输入,它将禁用其他输入标签,从而禁用其他输入标签输入本身。我的问题是最后一个输入“ text3”将没有id传递给函数,并且将该变量声明为null,我如何才能为其赋予一个“ fakeID”。 PS:请记住我是javascript新手。
<select id="text1" onchange="getIDDataChange(this.id)">
<option></option>
<option value="0">default</option>
</select>
<input type="text" id="text2" onkeyup="getIDDataChange(this.id)" disabled="disabled">
<input type="text" id="text3" onkeyup="getIDDataChange(this.id)" disabled="disabled">
<script language="javascript">
function getIDDataChange(givenID){
var idArray = ['text2', 'text3']
if (givenID == "text1"){
validChange(givenID,idArray[0],idArray[1]);
}
if (givenID == "text2"){
validChange(givenID,idArray[1]);
//the last ID is a problem as there is no value for the third id in validChange()
}
}//gives variables to the change function
function validChange(firstID,secondID,thirdID) {
var type = document.getElementById(firstID).type;
var min = 0;
if(type == "select-one")//if the box entered into is select
{
if (document.getElementById(firstID).value != "") {
document.getElementById(secondID).disabled = false;
}
else{
document.getElementById(secondID).disabled = true;
document.getElementById(thirdID).disabled = true;
//disables inputs after itself if the firstID is empty
}
}
else if(type == "text")//if the box entered into is text
{
if (document.getElementById(firstID).value.length > min){
document.getElementById(secondID).disabled = false;
//enables the element after itself
}
else{
document.getElementById(secondID).disabled = true;
document.getElementById(thirdID).disabled = true;
//disables inputs after itself if the firstID is empty
}
}
}
//once input from text1 is entered launch this function
</script>