2012-07-28 37 views
0

有兩個選擇按鈕需要客戶端驗證。用戶必須選擇其中一個,如果用戶選擇其中一個,另一個將自動禁用。如何使用JavaScript禁用HTML表單元素?

這是我的腳本:

<html> 
    <head> 
     <script type="text/javascript"> 
     function Check() //this the funciton for diasbled 
    { 
     var x =document.getElementById("supexp1"); 
     var y =document.getElementById("supexp2"); 

     if(x.value != "") 
     { 
     document.form1.supexp2.disabled=true; 
     } 
     else if(y.value != "") 
     { 
     { 
     document.form1.supexp1.disabled=true; 
     } 
     } 
    } 

     </script> 

    </head> 
    <body> 
    <form method="POST" action="destination.php" id="form1"> 
     <select name="SUPEXP" id="supexp1" data-native-menu="false" onChange="Check()"> 
      <option >Ekspedisi Local1</option> //this selection 1 
      <option >Ekspedisi Local2</option> //this selection 1 
      <option >Ekspedisi Local3</option> //this selection 1 

     </select> 
    <select name="SUPEXP" id="supexp2" data-native-menu="false" onChange="Check2()"> 
      <option >Ekspedisi Expor2</option> //this selection 2 
      <option >Ekspedisi Expor2</option> //this selection 2 
      <option >Ekspedisi Expor2</option> //this selection 2 


    </select> 
    </form> 
    </body> 
</html> 

我使用jquerymobile框架和選擇連接到數據庫。該代碼(JavaScript)未運行。

+1

你在哪裏導入jquerymobile? – 2012-07-28 04:15:39

+0

@LarryBattle我支持標籤之間的Jquery手機..,有什麼問題嗎? – MudMan23 2012-07-28 04:19:58

回答

1

我想獲得所選項目的價值,你需要使用一些這樣的事

var e = document.getElementById("supexp1"); 
    var x = e.options[e.selectedIndex].value; 

,而不是

var x =document.getElementById("supexp1"); 

做同樣的Y的

+0

謝謝,我會試試 – MudMan23 2012-07-28 04:22:45

+0

告訴我,如果它的工作:) – 2012-07-28 04:26:02

+0

它不工作,和mybe我有一個錯誤如何把它在scipt? – MudMan23 2012-07-28 04:35:01

3

我希望這是你試圖根據你的描述在這裏做的是jsfiddle

$(document).ready(function() { 
     $('#supexp1').change(function() { 
     $('#supexp2').attr("disabled" , "disabled") 
     }); 
     $('#supexp2').change(function() { 
     $('#supexp1').attr("disabled" , "disabled") 
     });  
    });​ 
+0

謝謝你的回答,它如果我不使用jquery手機,在js小提琴中,如果我檢查jqm,它不工作。 – MudMan23 2012-07-30 00:46:15

1

試試這個代碼

if(x.value != ""){ 
    document.getElementById("supexp2").disabled=true; 
} else if(y.value != "") { 
    document.getElementById("supexp1").disabled=true; 
}