簡體   English   中英

Javascript / HTML如何使用下拉框禁用下拉框

[英]Javascript/Html How to disable a dropdown box with a dropdown box

只是想知道有人在未選擇一個選項的情況下如何禁用下拉框的例子嗎?

例如

<td class="Item">State(Aus only):</td>
<td class="Data">
  <select class="TextBox" name="State" id="selectState">   
   <option>New South Wales</option>
   <option>Northern Territory</option>
   <option>Queensland</option>
  </select>
 </td>



 <td class="Item">Country:</td>
 <td class="Data">
   <select class="TextBox" name="Country" id="selectCountry">
    <option value="Australia" id="Australia" name="Australia">Australia</option>
    <option value="Braz">Brazil</option>
   <option value="China">China</option>
  </select>     
 </td>

   <script>
   function selected()
if(document.selectCountry.checked)      
{
document.getElementById("selectCountry.Australia").disabled = false;
    }   
else
    {   
document.getElementById("selectCountry.Australia").disabled = true;
}
  </script>

我希望使用JAVASCRIPT禁用“州”下拉菜單,除非選擇“國家/地區”為澳大利亞。.請問我能得到一些幫助嗎?

我還是整個Javascript編碼的新手,如果我的代碼不好,對不起,但我希望你能回答我的問題

您需要像這樣修復您的js代碼

function selected(){
 if(document.getElementById("selectCountry").value == "Australia") {
   document.getElementById("selectState").removeAttribute("disabled");
 }   
 else {   
   document.getElementById("selectState").setAttribute("disabled","disabled");
 }
}

這是上面代碼的小提琴

更新了小提琴 ,如果默認情況下國家/地區不是“ Australia”,則在頁面加載時調用該函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM