最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery append with dropdown option in disabled="disabled" - Stack Overflow

programmeradmin3浏览0评论

In my normal html drop down menu I am setting the default selected option to disabled state using below code

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

I have one mor drop down menu where the options are getting populated depends upon first drop down selection.

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

and using below 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>");       
      }
  });   
});

Here also, I need to set the default selected option Select Your product as disabled similar to first.I have tried to put

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

But that is not working. How to do it ?

FS FIDDLE SETUP

In my normal html drop down menu I am setting the default selected option to disabled state using below code

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

I have one mor drop down menu where the options are getting populated depends upon first drop down selection.

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

and using below 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>");       
      }
  });   
});

Here also, I need to set the default selected option Select Your product as disabled similar to first.I have tried to put

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

But that is not working. How to do it ?

FS FIDDLE SETUP

Share Improve this question edited Jan 10, 2014 at 14:44 Vikas Arora 1,6642 gold badges17 silver badges39 bronze badges asked Jan 10, 2014 at 14:42 acracr 1,74611 gold badges49 silver badges83 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

JSFIDDLE DEMO

You're emptying and then appending again. But you didn't add disabled option

     $("#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>");

         }
     });

You need to add disable='disabled' for default option and note that 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

I use this.

$("#sel_state").empty().append($('<option>', {
   disabled: true,
   value: value,
   text: text
}));
发布评论

评论列表(0)

  1. 暂无评论