Script is not working in IE

Asked

Viewed 1,694 times

8

I’m using this script in Chrome and it works normally. But in IE 8 or 9 it doesn’t work. I put a Debugger and an Alert to debug, but it doesn’t even enter the function.

$(document).on("mousedown", '#CentroCusto_new option', function (event) {
    alert('oi');
    this.selected = !this.selected;
    event.preventDefault();
});

Someone knows the reason and how to fix?

Jsfiddle

  • Tried to simply return falseinstead of setting returnValue? jQuery normalizes that.

  • Another thing: you need to be with the IE dev tools open, or the first line (debugger) is gonna suck.

  • Does the rest of the program work normally? (the part that depends on jQuery, mainly) Is there any other working code that uses on? And a long shot: ever tried to put your Handler in the body or some other ancestral element of #CentroCusto_new, instead of the document?

  • In IE it cannot even enter the function, nothing inside will work. @bfavaretto

  • Now I noticed that you are trying to get clicks on options. Why not monitor the select change instead?

  • It didn’t work on the body. I put it in the option because I am preventing his default behavior that is to deselect others by clicking on one.

  • I think the ie is not accepting the selector

  • 1

    I know it sounds crazy, but try putting an attribute size with numerical value in its select. See http://stackoverflow.com/a/3341779/825789

  • It didn’t work. I’m here without knowing what else to try.

  • I created a Jsfiddle. http://jsfiddle.net/fp4WD/

  • I’m on Linux right now, but as I recall, IE doesn’t trigger the click in option but rather in the select. Try attaching the event to click to your select and collects the value of option: Jsfiddle Aqui

  • I tested on IE8 and IE9, there is no way you can capture the event by clicking on an option, but you can capture in select, the event will not tell you who is selected, but you can find out in the properties of the element.

  • regarding your Jsfiddle, I see that IE has problems with the version of jQuery 1.10.1, see the error it gives on the console: SCRIPT5: Acesso negado.
jquery-1.10.1.js, linha 1513 Caractere 2
SCRIPT5009: '$' não está definido 
_display, linha 21 Caractere 5


  • believe it or not, the order Voce calls the scripts in your html interfere in whether or not the IE’s work, something else, each function needs a separate file because if not the IE gets lost, discovered this when I had a Jquery function file of over 100 lines and IE didn’t even read the file...

  • I saw this problem in stackoverflow in English, can you adapt my code with the accepted answer code? I tried but I don’t think so, because I need the default option Prevent. http://stackoverflow.com/questions/3341740/click-event-for-option-doesnt-work-in-ie

  • I was able to solve this problem? Looking quickly at the idea that occurs to me is: http://jsfiddle.net/k4G3J/

Show 11 more comments

3 answers

7

Following the comment I left yesterday on your question, today I had the opportunity to test and really Internet Explorer does not trigger the event of click in option of the element select but in the very select.

So that you can trigger a certain action when a option and altered, you can attach the event click à select and search for the option that you are selected to act in accordance with it:

Demonstration at Jsfiddle

// anexar evento de click à select
$(document).on("click", 'select', function (e) {

    // apanha option que recebeu click
    var $opt = $(this).find(":selected");

    // fazer algo com o valor da mesma
    alert($opt.attr("value"));
}); 
  • Searching also found this way. But I could not adapt it to my code using preventDefault. + 1 as it may be useful in other cases

  • But what is your specific objective? The default behavior of a multiple selection box is to select something or make a multiple selection. It’s selection you want to avoid?

  • The default behavior is that at mouse click you only select one option at a time. Needing to press Ctrl and click for it to select more than one. I want to have the same result as Ctrl + Click just by clicking with the mouse.

0


IE doesn’t really let you understand selection through option.

ie understands the $(":Selected") selector, which may be useful in some cases, but not in this specific case.

0

It works ?

    jQuery("#CentroCusto_new option").on('click', function (event) {
        event.preventDefault();
        alert('oi');
        this.selected = !this.selected;
        return false;
    });

or something like that ?

$( ".target" ).change(function() {
alert( "Handler for .change() called." ); 
});

http://api.jquery.com/change/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.