简体   繁体   English

已选择选项时触发功能

[英]Trigger function when already selected option selected

I want to call a function when already selected OPTION is select from drop-down. 当从下拉列表中选择已经选择的OPTION时,我想调用一个函数。

 $("#dropdown1").on("change",function(){ alert($(this).val()); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="dropdown1"> <option value="one"> One </option> <option value="two"> Two </option> <option value="three"> Three </option> </select> 

In above code, when i select option ONE than ALERT works. 在上面的代码中,当我选择选项ONE时,ALERT起作用。 So, now option ONE is selected. 因此,现在选择了选项ONE。 But i want that when i select option ONE again, than function should call again. 但是我希望当我再次选择选项ONE时,功能应该再次调用。

Your event is change, if you select the same value again then there is no change, so no alert is fired.Introduce a dummy option which is hidden. 您的事件是更改,如果再次选择相同的值,则没有更改,因此不会触发警报。引入一个隐藏的虚拟选项。 Make all the calculations/manipulation you want to do with the selected value and then change the value to the dummy option element 使用选定的值进行所有要进行的计算/操作,然后将该值更改为虚拟选项元素

 $("#dropdown1").on("change",function(){ alert($(this).val()); $(this).val(""); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="dropdown1"> <option value="" disabled style="display:none;">Select</option> <option value="one" selected> One </option> <option value="two"> Two </option> <option value="three"> Three </option> </select> 

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

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