jQuery, $(element).click doesn't work on ie<

2019-07-15 01:13发布

问题:

I have a click event with jQuery like that :

$(document).ready(function() {    
    $("#id_secteur_activite").click(function () {    
        console.log('ok');    
    });
});

On firefox and ie9, when i click on my element #id_secteur_activite, i have the message "ok" in my console.

But when i test on ie 8 and ie7, i have nothing. .click doesn't work on ie<9.

Do you have any ideas ? thanks !

Edit :

<SELECT id=id_secteur_activite name=secteur_activite>
<OPTION selected value="">choisissez d'abord un secteur d'activité</OPTION>
</SELECT>

回答1:

use .on()

$(document).ready(function() {    
    $("#id_secteur_activite").on('click',function () {
        alert('ok');    
    });
});

and also use alert instead of console.



回答2:

[DEMO(http://jsfiddle.net/mplungjan/8t8Jh/show/)

All works using a high enough version of jQuery (here edge which is currently v1.9.2pre)

$(document).ready(function() {    
/*    $("#id_secteur_activite").click(function () {    
        console.log('click');    
    });
*/    
    $("#id_secteur_activite").on("click",function () {    
        console.log('click');    
    });
    $("#id_secteur_activite").on("change",function () {    
        console.log('change');    
    });
});


回答3:

you can use both click event and change event.

$(document).ready(function() {    
    $("#id_secteur_activite").on('change',function () {    
        console.log('ok');    
    });
});