检查是否已单击或更改元素 [英] Check if element has been clicked or changed

查看:90
本文介绍了检查是否已单击或更改元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个愚蠢的问题,我想我已经知道答案了,但是我想从一个比我了解更多jquery知识的人那里找到答案. 我有一个下拉列表,我想知道是否可以检查DDL是否已单击或更改.例子

I have some what of a stupid question and I think I already know the answer but I would Like to find out from someone with more jquery knowledge than I have. I have a drop down list and I would like to know if I can check to see if the ddl has been clicked or changed. Example

If($('#ddl').click() || $('#ddl').on('change'){
    //do something.
}

推荐答案

您可以将click和change事件处理程序绑定到该元素并设置一个标志:

You can bind a click and change event handler to the element and set a flag:

$('#ddl').on('click change', function() {
    $(this).data('clicked', true);
});

// later
if ($('#ddl').data('clicked')) {
   // ...
}

当然,如果您想在单击或更改元素时执行操作,请将该代码直接放在事件处理程序中.在这种情况下,您不需要标志.

Of course if you want to perform an action when the element is clicked or changed, put that code directly in the event handler. You don't need a flag in that case.

这篇关于检查是否已单击或更改元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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