开发者

hide() not working in IE

My code works perfect in firefox and gives 开发者_运维知识库error in IE. any ideas?

I have a dropdown with various options, I am trying to show/hide options in another dropdown based on the selected value.

function selectNames() {
var Name = $("#SelectName").attr("value");
 $("."+Name).each(function() {
   $(this).hide();            
 });
}
<select >
   <option class="Name1" value="SomeName1" </option>
   <option class="Name2" value="SomeName2" </option>
</select>
<select id="SelectName" onchange="javascript:selectNames();" >
   <option value="Name1" </option>
   <option value="Name2" </option>
</select>

Any help is appreciated..


Make sure you close the start tag. Try to use this:

<select>
    <option class="Name1" value="SomeName1" />
    <option class="Name2" value="SomeName2" />
</select>
<select id="SelectName" onchange="javascript:selectNames();" >
    <option value="Name1" />
    <option value="Name2" />
</select>

Seems to work for me in IE8.


It won't work in IE & Chrome

check out in IE or Chrome

The best alternative that you can do is to remove the option rather than hiding it.(you should keep a copy of the original options before removing it.)

var copy = $("."+Name).clone();
function selectNames() {
   $("#thefirstselect option").remove();
   copy.appendTo("#thefirstselect");
   var Name = $("#SelectName").val();

   $("."+Name).each(function() {
      $(this).remove();            
});
}


Your markup is not correct. You are each option open tag isn't properly closed.

Also, the specs do not specify CSS changes to individual option tags, though it does work on Firefox.

In simpler words, you cannot hide individual inputs - in which case, you'll have to remove them.


If this is a direct copy and paste then you need to close the select options to look like this:

<option value="Name1">Name1</option>
<option value="Name2">Name2</option>


I would suggest having two selects that you show and hide. Show and hide of options sounds risky.

Also, make sure you set the hidden select to attr('disabled','disabled')/disabled="disabled" and then when you unhide it undo that with removeAttr('disabled'). This is to prevent the hidden select from posting data to the server when you have multiple selects with the same name="...".

If you must use a single select, you may want to appendTo/remove the options, but that is up to you. If show/hide works in all browsers, go for it.


Sadly, you just can't.
IE don't support hide of individual options in a select, neither Chrome or Opera. This feature isn't cross browser.

What you can do is remove the option and add it again later...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜