每次使用jQuery选择DropDownList项目时都会触发事件 [英] Fire event each time a DropDownList item is selected with jQuery

查看:242
本文介绍了每次使用jQuery选择DropDownList项目时都会触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表:<asp:DropDownList id="dropdownid" runat="server" class=blah"/>

在我的jQuery中,我分配如下更改事件:

in my jQuery, I assign change event like this:

$('#dropdownid').change(function() {......});

现在,当我从下拉列表中选择不同的值时,此方法有效,但是,假设我要再次选择相同的值. (因为我想拨打另一个具有相同值的电话) 因此,当我再次选择它(不更改值)时,只需从下拉列表中单击选定的值并再次选择"它,就不会触发任何事件. 我必须分配给jquery中的另一个事件吗?有什么解决方法?

Now, this works when I select different value from the dropdown, however let's say I want to select the same value again. (because I want to make another call with the same value) So, when I select it again, (without changing the value) just by clicking on the selected value from the dropdown and "selecting" it again, no event fires. Is there another event in jquery that I have to assign to it? what's the workaround?

推荐答案

展开

To expand Vincent Ramdhanie's suggestion, take a look at doing something like this. Essentially, you end up with your own jQuery function that you can re-use elsewhere.

第1步:创建jQuery函数

Step 1: Create the jQuery Function

(function($) {
    $.fn.selected = function(fn) {
        return this.each(function() {
            var clicknum = 0;
            $(this).click(function() {
                clicknum++;
                if (clicknum == 2) {
                    clicknum = 0;
                    fn(this);
                }
            });
        });
    }
})(jQuery);

第2步:确保引用了新创建的jQuery Function的文件以供使用:

Step 2: Make sure that the newly created jQuery Function's file is referenced for use:

<script src="Scripts/jqDropDown.js" type="text/javascript"></script>

第3步:使用新功能:

$('#MyDropDown').selected(function() {
    //Do Whatever...
});

原始信息
使用当前的代码库,从asp:DropDownList中选择相同的值将不会触发更改事件.

ORIGINAL INFO
With your current code base, selecting the same value from the asp:DropDownList will not fire the change event.

您可以尝试为.blur事件添加另一个jQuery函数.当控件失去焦点时就会触发:

You could try adding another jQuery function for the .blur event. This would fire when the control loses focus:

$('#dropdownid').blur(function() {......});

如果模糊功能对您不起作用,我将添加一个刷新按钮或一些会触发您尝试使用的功能的东西.

If they blur function doesn't work for you, I'd add a refresh button or something to that affect that fires the function you are trying to utilize.

这篇关于每次使用jQuery选择DropDownList项目时都会触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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