javascript jQuery - 用于检测是否已选择 HTML 选择选项的事件处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7155087/
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 - event handler to detect if HTML select option has been selected
提问by user906568
How can I detect in jQuery when any option in a select tag has been changed?
如何在SELECT标记中的任何选项已更改时检测jQuery?
For example [pseudocode]:
例如[伪代码]:
event handler function -> option changed(){
//do some stuff
}
EDIT:
编辑:
I have the following select:
我有以下选择:
<select id = "selectattribute">
<OPTION VALUE="0">Choose Attribute</OPTION>
<?php echo($options);?>
</select>
I am trying this in jQuery:
我正在 jQuery 中尝试这个:
$(document).ready(function() {
$("#selectattribute").change(function() {
alert('Handler for .change() called.');
});
});
Nothing happens...the function isn;t triggering.
什么都没有发生……该功能没有触发。
EDIT: it works when I use $("#select")
instead of $("#selectattribute")...can you not apply it to particular select tags?
编辑:当我使用$("#select")
而不是$("#selectattribute")...can you not apply it to particular select tags?
回答by Jon Adams
From jQuery docs regarding the change handler:
<form>
<input class="target" type="text" value="Field 1" />
<select class="target">
<option value="option1" selected="selected">Option 1</option>
<option value="option2">Option 2</option>
</select>
</form>
<div id="other">
Trigger the handler
</div>
The event handler can be bound to the text input and the select box:
事件处理程序可以绑定到文本输入和选择框:
$('.target').change(function() {
alert('Handler for .change() called.');
});
回答by Jon P
Your Code works in Firefox using the mouse, see this fiddle.
您的代码使用鼠标在 Firefox 中工作,请参阅此小提琴。
However using the keyboard is a different matter as per this bug report
但是,根据此错误报告,使用键盘是另一回事
To work around this I've added a key up handler to remove focus from the element then re-focus, as per this fiddle
为了解决这个问题,我添加了一个 key up 处理程序来从元素中移除焦点,然后重新聚焦,按照这个小提琴
If the change event is not triggering when using the mouse, check the options rendered by your php code. Look for structures that might be "breaking" the select tag.
如果使用鼠标时未触发更改事件,请检查您的 php 代码呈现的选项。寻找可能“破坏”select 标签的结构。
回答by cpjolicoeur
$("select").change(function() {})
回答by jerjer
Try bind:
尝试绑定:
$("#selectattribute").bind('change', function() {
alert('Handler for .change() called.');
});
or change function in jQUery:
或更改 jQUEry 中的功能:
$("#selectattribute").change(
function() {
alert('Handler for .change() called.');
}
);
回答by wukong
Maybe there is another tag which id is named "selectattribute".The tag id should be unique.If there is more than one id,jQuery will use the first one founded.
可能还有一个标签名为“selectattribute”。标签id应该是唯一的。如果有多个id,jQuery会使用第一个创建的。
回答by Feras
Looks like your JQuery is not loaded,check first that it is loaded
看起来您的 JQuery 未加载,首先检查它是否已加载
//case if mini jQueryis not used replace with window.jQuery
if (window.$) {
console.debug('jQuery is loaded ');
} else {
console.debug('jQuery is not loaded ');
}