jQuery获取生成元素属性FF vs IE问题
我的选择标签(filterBA)上有一个点击事件,然后动态生成选项。
$("#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());
}
}
});
这在 Firefox 中效果很好,但 IE 不会将我的 $tgt 识别为选项。我通过添加来检查:
$("#filterBA").click(function(event){
var $tgt = $(event.target);
alert($tgt.is('option'));
...
并且无论它返回 false。但在 FF 中它返回 true vi 我按住鼠标选择其中一个选项。我不明白,因为当我必须选择我创建的自定义选择列表(在同一系统中)中使用的 div 时,我使用相同的方法,但是当点击事件位于标签上时,它不会工作。
有人知道可能出了什么问题吗?
顺便提一句。这是我生成选项的函数:
function pupulateBA(){
$selectTag = $('#nav select').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.');
}
});
}
我找到了一个解决方案:
ofc 我是个白痴,因为我忘记了所有关于“:selected”的事情.. DOH!这是我标签上的新事件:
$("#filterBA").change(function(){
$('#filterBA option:selected').each(function(){
alert($(this).attr('id'));
});
});
这适用于 FF 和 IE 在选择下拉列表中动态创建的选项。
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 select').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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自QuirksMode。
不积极如何解决它。不过,请阅读 QuirksMode 文章,因为它提供的函数可能会或可能不会执行您想要的操作,并且还指定了一些希望对您有用的其他属性。尽管我希望 jQuery 有一个抽象版本,这样您就不必担心 IE 黑客攻击。
From QuirksMode.
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.