Javascript jquery 选择选项单击处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5749597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery select option click handler
提问by john
given:
给出:
<select id="mySelect">
<option>..</option>
...
</select>
Using the select id, how can I trigger a click event on one of the options? I tried attaching the event directly to the select, but this triggers an event whenever the select is clicked on (even if there are no options). Oh, and it is a multi-select (although I don't think that matter).
使用选择 id,如何在其中一个选项上触发点击事件?我尝试将事件直接附加到选择,但是每当点击选择时都会触发一个事件(即使没有选项)。哦,它是一个多选(虽然我认为这无关紧要)。
回答by Matt
You want the 'change' event handler, instead of 'click'.
您需要“更改”事件处理程序,而不是“单击”。
$('#mySelect').change(function(){
var value = $(this).val();
});
回答by Zombyii
$('#mySelect').on('change', function() {
var value = $(this).val();
alert(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<select id="mySelect">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
</select>
回答by tbradley22
you can attach a focus event to select
您可以附加一个焦点事件来选择
$('#select_id').focus(function() {
console.log('Handler for .focus() called.');
});
回答by Matthew Dolman
The problem that I had with the change
handler was that it triggered on every keypress that I scrolled up and down the <select>
.
我在change
处理程序上遇到的问题是它在我上下滚动<select>
.
I wanted to get the event for whenever an option was clicked or when enter
was pressed on the desired option. This is how I ended up doing it:
我想在单击选项或enter
按下所需选项时获取事件。这就是我最终这样做的方式:
let blockChange = false;
$element.keydown(function (e) {
const keycode = (e.keyCode ? e.keyCode : e.which);
// prevents select opening when enter is pressed
if (keycode === 13) {
e.preventDefault();
}
// lets the change event know that these keypresses are to be ignored
if([38, 40].indexOf(keycode) > -1){
blockChange = true;
}
});
$element.keyup(function(e) {
const keycode = (e.keyCode ? e.keyCode : e.which);
// handle enter press
if(keycode === 13) {
doSomething();
}
});
$element.change(function(e) {
// this effective handles the click only as preventDefault was used on enter
if(!blockChange) {
doSomething();
}
blockChange = false;
});
回答by milad fallahi
its working for me
它为我工作
<select name="" id="select">
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
</select>
<script>
$("#select > option").on("click", function () {
alert(1)
})
</script>
回答by VladL
One possible solution is to add a class to every option
一种可能的解决方案是为每个选项添加一个类
<select name="export_type" id="export_type">
<option class="export_option" value="pdf">PDF</option>
<option class="export_option" value="xlsx">Excel</option>
<option class="export_option" value="docx">DocX</option>
</select>
and then use the click handler for this class
然后使用这个类的点击处理程序
$(document).ready(function () {
$(".export_option").click(function (e) {
//alert('click');
});
});
UPDATE:it looks like the code works in FF, IE and Opera but not in Chrome. Looking at the specs http://www.w3.org/TR/html401/interact/forms.html#h-17.6I would say it's a bug in Chrome.
更新:看起来代码在 FF、IE 和 Opera 中有效,但在 Chrome 中无效。查看规范http://www.w3.org/TR/html401/interact/forms.html#h-17.6我会说这是 Chrome 中的一个错误。
回答by Igor Aleksic
What I have done in this situation is that I put in the option elements OnClick event like this:
在这种情况下,我所做的是将选项元素放入 OnClick 事件中,如下所示:
<option onClick="something();">Option Name</option>
Then just create a script function like this:
然后只需创建一个这样的脚本函数:
function something(){
alert("Hello");
}
UPDATE:
Unfortunately I can't comment so I'm updating here
TrueBlueAussieapparently jsfiddleis having some issues, check here if it works or not: http://js.do/code/klm
更新:不幸的是,我无法发表评论,所以我在这里更新
TrueBlueAussie显然jsfiddle有一些问题,请在此处检查它是否有效:http: //js.do/code/klm