2014-01-10 63 views
0

在我正常的HTML下拉選項追加使用下面的代碼的jQuery在禁用=「禁用」

<select id="sel_bank" name="sel_bak"> 
    <option disabled="disabled" SELECTED >Select Your product</option> 
    <option>Mobile</option> 
    <option>laptop</option> 
</select> 

我有一個鐵道部下拉菜單下拉菜單中我設置默認選擇的選項爲禁用狀態,其中選項正在填充取決於第一個下拉選擇。

<select id="sel_state" name="sel_state"> 
    <option disabled="disabled" SELECTED >Select Your brand</option> 
</select> 

,並使用下面的jQuery:

$(document).ready(function() { 
    $("#sel_bank").change(function() { 
    var el = $(this) ; 
    if(el.val() === "Mobile") { 
    $("#sel_state").empty().append 
    ("<option SELECTED>Select Your product</option>\ 
     <option>Samsung</option>\ 
     <option>Nokia</option>"); 
    } 
    else if(el.val() === "laptop"){ 
    $("#sel_state").empty().append 
      ("<option SELECTED>Select Your product</option>\ 
      <option>HP</option>\ 
      <option>Dell</option>");  
     } 
    }); 
}); 

這裏也有,我需要設置選定的選項Select Your productdisabled類似first.I默認試圖把

("<option disabled="disabled" SELECTED>Select Your product</option>\ 
    <option>HP</option>\ 
    <option>Dell</option>"); 

但那是行不通的。怎麼做 ?

FS FIDDLE SETUP

回答

2

JSFIDDLE DEMO

你排空,然後再追加。但是你沒有添加disabled選項

 $("#sel_bank").change(function() { 
     var el = $(this); 
     if (el.val() === "Mobile") { 
      $("#sel_state").empty().append("<option disabled='disabled' SELECTED>Select Your product</option>\ 
    <option>Samsung</option>\ 
<option>Nokia</option>"); 
     } else if (el.val() === "laptop") { 
      $("#sel_state").empty().append 

      ("<option disabled='disabled' SELECTED>Select Your product</option>\ 
    <option>HP</option>\ 
<option>Dell</option>"); 

     } 
    }); 
1

您需要添加disable='disabled'爲默認選項,注意use single quotes

$(document).ready(function() { 
$("#sel_bank").change(function() { 
var el = $(this) ; 
if(el.val() === "Mobile") { 
    $("#sel_state").empty().append 
    ("<option disabled='disabled' SELECTED>Select Your product</option>\ 
    <option>Samsung</option>\ 
<option>Nokia</option>"); 
    } 
     else if(el.val() === "laptop") { 
     $("#sel_state").empty().append 

      ("<option disabled='disabled' SELECTED>Select Your product</option>\ 
    <option>HP</option>\ 
<option>Dell</option>"); 

     } 
    }); 


}); 

FIDDLE Demo