简体   繁体   English

如何在选择元素上触发点击事件? (没变)

[英]How to trigger click event on select element? (not change)

In the following code when I change the selection, there will be an alert . 在下面的代码中,当我更改选择时,将出现alert I am trying to make the function like when I click on the option then it will show an alert . 我正在尝试使功能类似于单击option ,它将显示alert

 $(document).ready(function() { $("#x").change(function() { alert("Haha"); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="x"> <option selected>A</option> <option>B</option> <option>C</option> </select> 

In the below code there is no effect when I click on the options already selected options. 在下面的代码中,当我单击已经选择的选项时,没有任何效果。 for example a is selected then i click a is no effect. 例如选择一个然后我单击一个是没有效果的。

 $(document).ready(function() { $("#x").on("option", "click", function() { alert("Haha"); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="x"> <option selected>A</option> <option>B</option> <option>C</option> </select> 

because i want to trigger event while i re-clicking the selected option. 因为我想在重新单击所选选项时触发事件。

click selection box->drop menu->click selected option->trigger event 单击选择框->下拉菜单->单击所选选项->触发事件

Can anyone help me? 谁能帮我?

Have you tried bind with select eg: 您是否尝试过与select绑定,例如:

$('#x').bind('click', function(){
    console.log('Clicked') 
 });

If this doesn't work do tell. 如果这不起作用,请告诉。 Thanks Hope this helps. 谢谢希望对您有所帮助。

Do you need to execute your code while clicking on Dropdown??? 您需要在单击下拉菜单时执行代码吗???

If Yes, here is the code for you 如果是,这是您的代码

https://jsfiddle.net/shoesheill/gjLyxo5d/6/ https://jsfiddle.net/shoesheill/gjLyxo5d/6/

If not please leave a comment along with your requirements. 如果没有,请与您的要求一起发表评论。

$(document).ready(function() {
  $("#x").off().on('click',function() {
    alert("Haha");
  });
});

"click selection box->drop menu->click selected option->trigger event" “单击选择框->下拉菜单->单击选定的选项->触发事件”

First of all do not use alert() , it prompts for an extra click you really don't need to waste your time on. 首先,不要使用alert() ,它会提示您进行额外的单击,您实际上不需要浪费时间。 Use console.log() . 使用console.log()

The following demo: 以下演示:

  • Delegates the click event to select#x : click事件委托给select#x

$('#x') .on ( 'click' ,... $('#x'). on'click' ,...

  • Once clicked, it will trigger a focus event on every even click : 单击后,它将在每次单击✱时触发focus事件

if (cnt % 2 === 0) { $(this) .trigger ('focus');} ✱if (cnt%2 === 0){$(this) .trigger ('focus');}

  • select#x is also delegated to the focus event and will call optionTrigger() : select#x也委托给focus事件,并将调用optionTrigger()

$('#x').on( 'focus' , optionTrigger); $('#x')。on( 'focus' ,optionTrigger);

  • function optionTrigger() will log the selected <option> index and text: function optionTrigger()将记录所选的<option>索引和文本:

if (cnt < 2) { ... ✱if (cnt <2){ ...

... $(this) .trigger( 'blur' ); ... $(this) .trigger( 'blur' ); } }

var idx = $(this) [0] .selectedIndex ; var idx = $(this) [0] .selectedIndex ;

var txt = $(this) .find ('option') .eq (idx) .text() ; var txt = $(this) .find ('option'). eq (idx) .text() ;


Demo 演示

 var cnt = 1; $("#x").on("click", function(e) { if (cnt % 2 === 0) { $(this).trigger('focus'); } cnt++; }); $('#x').on('focus', optionTrigger); function optionTrigger(e) { if (cnt < 2) { $(this).trigger('blur'); } else { var idx = $(this)[0].selectedIndex; var txt = $(this).find('option').eq(idx).text(); console.log(idx + ': ' + txt); } } 
 <select id="x"> <option>A</option> <option>B</option> <option>C</option> </select> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM