JavaScript代码无效

时间:2014-10-10 13:59:05

标签: javascript html

我正在尝试对其进行编码,就像当我取消勾选复选框时,两个下拉菜单都应该变为只读,但目前只有第二个下拉菜单变为只读,第一个下拉菜单不会被读取只有并且仍然可以编辑,这里有什么问题?为什么第一次下拉只有在取消勾选复选框后才会被读取?



 <html>
    <head>
    <script type="text/javascript">
    function myFunction() {
        document.getElementById("1").disabled = false;
    }
    
    function mySecondFunction() {
     var ddl = document.getElementById("1");
     var selectedValue = ddl.options[ddl.selectedIndex].value;
        if (selectedValue == "ExceptionZero")
       {
        document.getElementById("mySelection").disabled = true;
    	//set default
    	document.getElementById("mySelection").value = "OptionZero"
       }else {
        document.getElementById("mySelection").disabled = false;
       }
     
    }
    
    function myThirdFunction(){
    	document.getElementById("mySelection").disabled = true;
    	//set default 
    	document.getElementById("mySelection").value = "OptionZero"
    	
    	//first drop down 
    	document.getElementById("1").disabled = true;
    	document.getElementById("1").value = "OptionZero"
    }
    
    </script>
    
    </head>
    <body>
    <input type="checkbox" name="selectionbox" value="Yes" onclick="myFunction()" onchange="myThirdFunction()"/>
    
    <select disabled id="1" onchange="mySecondFunction()">
    				<option value="OptionZero">Default</option>
    				<option value="OptionOne">Car</option>
    				<option value="OptionTwo">Car2</option>
    				<option value="OptionThree">Car23</option>
    </select>
    <br>
    	<select disabled id="mySelection">
    				<option value="OptionZero">Default</option>
    				<option value="OptionOne">A</option>
    				<option value="OptionTwo">B</option>
    				<option value="OptionThree">C</option>
    	</select>
    </body>
    </html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

onChange事件后再次调用onClick事件,导致框1启用。尝试将您的onclick更改为此并删除您的thirdFunction事件。

JSFiddle

function myFunction() {   
        if (document.getElementById("1").disabled == false) {
            document.getElementById("mySelection").disabled = true;
            //set default 
            document.getElementById("mySelection").value = "OptionZero"

            //first drop down 
            document.getElementById("1").disabled = true;
            document.getElementById("1").value = "OptionZero"    
        } else {
            document.getElementById("1").disabled = false;
        }
    }

事件调用的顺序可能因浏览器而异,因此当订单对输出产生影响时,您应该只有一个事件。