javascript 如何触发组合框的 onchange 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26587571/
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
How to fire onchange event of combobox?
提问by k00989
I have a combobox, How to fire onchange event of the combobox and get a selected value from it.
我有一个组合框,如何触发组合框的 onchange 事件并从中获取选定的值。
Here is the Code what i did till now :
这是我到目前为止所做的代码:
<select name="g1" id="select_g1">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
<script>
$(document).ready(function(){
$("select_g1").change(function(){
alert("Handled"); // alert is not fired up ...
});
});
</script>
EDIT :What if i have more than one combobox :
编辑:如果我有多个组合框怎么办:
<select name="g2">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
Also disable the value selected in combobox g1 in combobox g2.
还要禁用在组合框 g2 中的组合框 g1 中选择的值。
Could anyone please tell me what is wrong with the code.
谁能告诉我代码有什么问题。
回答by Satwik Nadkarny
Rather than trying to use the .change event, please try to use the .on event.
与其尝试使用 .change 事件,不如尝试使用 .on 事件。
You can try to use it as such:
您可以尝试这样使用它:
$('#select_g1').on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
alert(valueSelected);
});
See this here -> http://jsfiddle.net/ocrozmkn/
在这里看到这个 - > http://jsfiddle.net/ocrozmkn/
EDIT :
编辑 :
With respect to your edited question, you can try the following jQuery :
关于您编辑的问题,您可以尝试以下 jQuery :
$('select').on('change', function (e) {
$('select option').prop('disabled', false);
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
alert(valueSelected);
$("select option:contains('" + valueSelected + "')").attr("disabled","disabled");
});
See this here ->http://jsfiddle.net/ocrozmkn/1/
在这里看到这个 - > http://jsfiddle.net/ocrozmkn/1/
Demo for multiple comboboxes
多个组合框的演示
See here -> http://jsfiddle.net/ocrozmkn/6/
见这里-> http://jsfiddle.net/ocrozmkn/6/
Hope this helps!!!
希望这可以帮助!!!
回答by Jenson M John
Try
尝试
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Autocomplete</title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#select_g1").on("change",function(){
alert($(this).val());
});
});
</script>
</head>
<body>
<select name="g1" id="select_g1">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
</body>
</html>
回答by AkshayP
Try something like this :
尝试这样的事情:
<form class="demo">
<select name="g1" id="select_g1">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
</form>
<script>
$("#select_g1").change(function () {
var res = this.value;
alert(res);
//OR
alert(this.value);
});
</script>