jQuery get generated elements attributes FF vs IE problem
I have a click event on my select-tag (filterBA), and then the options are generated dynamicly.
$("#filterBA").click(function(event){
var $tgt = $(event.target);
if($tgt.is('option')){
if($tgt.attr('id') != 0){
sortRowsByBA($tgt.attr('id'));
}else{
appendOrReplaceRows("replace", getSortColumn(), getAscdesc());
}
}
});
This works great... in firefox, but IE won't reconnize my $tgt as an option. I check by adding:
$("#filterBA").click(function(event){
var $tgt = $(event.target);
alert($tgt.is('option'));
...
And no matter what it returns false. But in FF it return true vi i hold my mouse down a selects one of the options. I don't get it, since i use the same approach when i have to select a div used in a custom select-list i created (in the same system), but when the click-event is on the tag it won't work.
Anybody know what might be wrong?
btw. here is my function to generate the options:
function pupulateBA(){
$selectTag = $('#nav selec开发者_开发问答t').html('<option id="0">Select business area</div>');
$.ajax({
url: "ajaxBAs.cgi",
dataType: 'json',
success: function(jsonData){
for(var businessIndex in jsonData){
$selectTag.append("<option id='"+jsonData[businessIndex].id+"'>"+jsonData[businessIndex].name+"</option>");
}
},
error: function(){
alert('Ajax error retriving business areas to frontpage.');
}
});
}
I found a solution:
ofc im an idiot, since I forgot all about ":selected".. DOH! So here is my new event on my tag:
$("#filterBA").change(function(){
$('#filterBA option:selected').each(function(){
alert($(this).attr('id'));
});
});
This works with both FF and IE on dynamically created options in a select-dropdown.
From QuirksMode.
event.target - W3C/Netscape says: the target. No, says Microsoft, the srcElement.
Not positive how to work around it. Read the QuirksMode article though as it provides a function that may or may not do what you want it to do and also specifies a few other attributes that will hopefully work for you. Although I would hope that maybe jQuery had an abstracted version so that you don't have to worry about IE hacks.
精彩评论