ajax的单击事件不触发的选择选项 [英] ajax click event not firing for select option

查看:171
本文介绍了ajax的单击事件不触发的选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择列表(#searchHistory)阿贾克斯附:

I have a select list(#searchHistory) appended by ajax:

var toAppend = '<option rel="' + relval + '">' + eVal + '</option>';
$("#searchHistory").append(toAppend);  

和没有注册的选择框单击事件

and there is a click event registered for the select box

 $('#searchHistory option').click( function() {
    var shOpt = $(this);
    var relVal = shOpt.attr('rel');

    alert(relVal);
});

我的问题是,最后一个项目的选择框不会触发click事件!!!!所有其他项目被点击时工作。有趣的是,当我使用prePEND而不是在追加,第一个项目不会触发click事件和所有其他项目做的工作。请注意,选择框被阿贾克斯code填充上方。这意味着,如果我添加另一个到列表框中,则previous非工作项目运作良好,但最近添加的项目仍然无法正常工作。

my problem is that the Last Item of the select box does not fire the click event!!!! all other items are working when clicked. Interestingly when i use a PREPEND instead on APPEND, the first item does not fire the click event and all other items do work. please note that the select box is populated by ajax code above. it means, if i add another to the list box, then the previous non-working item works well but the recently added item still not work.

任何想法,请??? 谢谢

any ideas please ??? thanks

推荐答案

尝试

$('#searchHistory').change( function() {
    var shOpt = $(this).find('option:selected');
    var relVal = shOpt.attr('rel');

    alert(relVal);
});

这篇关于ajax的单击事件不触发的选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆