开发者

Weird IE problem using this jquery / javascript

开发者 https://www.devze.com 2023-02-08 20:03 出处:网络
I am using the following code: ieLessThan8OptionDisable = function() { if ($.browser.msie && parseFloat($.browser.version) < 8) {

I am using the following code:

ieLessThan8OptionDisable = function() {
if ($.browser.msie && parseFloat($.browser.version) < 8) {
    $("select").find("[disabled]").addClass("disabledforie").removeAttr("disabled");
    $("select").change(function(){
        var selected = $(this).val();
        var disabled = $(this).find("[value="+selected+"]").hasClass("disabledforie");
        if (disabled) {
            alert("This option is disabled.\nSelect will be set to the first option.");
            $(this).find("option:first").attr("selected","selected");
         }
        });
    }
}

Basically this code is for disabled option in a select drop down box. It works perfectly except there is a usability issue.

Anytime I click on the option which should be disabled in IE, an alert pops up, and after that th开发者_如何学Goe select box resets to the first position. All is fine. Now when I click on the select box to open the drop down, it just closes. Basically I have to click on it 2 times at which point it opens.

I have tried this in IE6 and IE7. Both have this issue.

Any pointers would be great!

Thanks


This sounds like a focus issue. The select box has focus when you select a new option, then you pop up an alert which steals focus away from the select element. IE should be closing the select box automatically when the alert gets called but alas they probably didn't test this edge case. So the two clicks do the following:

  1. Return focus to the select element
  2. Select an item in the list

Add a call to blur before you invoke the alert:

if (disabled) {
  this.blur();// add in
  alert("This option is disabled.\nSelect will be set to the first option.");
  $(this).find("option:first").attr("selected","selected");
}

PS - I haven't actually tested this, I don't have IE available right now

0

精彩评论

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