用户在下拉列表中进行选择时的 Jquery 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10353128/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:10:32  来源:igfitidea点击:

Jquery event when user makes selection in dropdown

jquery

提问by Nate Pet

I have a drop down list. In Jquery what is the event I would use when the user makes a selection.

我有一个下拉列表。在 Jquery 中,当用户进行选择时我会使用什么事件。

The id of the dropdown is drp1

下拉列表的id是drp1

I tried the following but did not work:

我尝试了以下但没有奏效:

$("#ddrp1").SelectChanged(SelectionItem);

回答by James Hill

Use the change()event:

使用change()事件:

$("#ddrp1").change(function() {
    // Pure JS
    var selectedVal = this.value;
    var selectedText = this.options[this.selectedIndex].text;

    // jQuery
    var selectedVal = $(this).find(':selected').val();
    var selectedText = $(this).find(':selected').text();
});

In jQuery 1.7, you can use .on()

在 jQuery 1.7 中,您可以使用 .on()

$("#ddrp1").on("change", function() {
    // Pure JS
    var selectedVal = this.value;
    var selectedText = this.options[this.selectedIndex].text;

    // jQuery
    var selectedVal = $(this).find(':selected').val();
    var selectedText = $(this).find(':selected').text();
}????);?

Here's a working jsFiddleusing on()

这是一个正在使用的jsFiddleon()

回答by Selvakumar Arumugam

What you want is onchangeevent which can be written as

你想要的是onchange事件,它可以写成

 $("#ddrp1").change (function () { 
 });

回答by ShankarSangoli

Use jQuery changeevent handler.

使用 jQuerychange事件处理程序。

$("#ddrp1").change(function(){
    //selection changed
    alert(this.value);//this will give the selected option's value
    alert($(this).find(':selected').text());//this will give the selected option's text
});

Alternative way to bind changeevent handler is.

绑定change事件处理程序的替代方法是。

$("#ddrp1").bind('change', function(){

});

回答by Dulith De Costa

You need to use change().

您需要使用change().

jQuery change event occurs when the value of an element is changed.

jQuery change 事件发生在元素的值改变时。

This event is limited to inputelements, textareaboxes and selectelements.

此事件仅限于input元素、textarea框和select元素。

$("#ddrp1").change (function () { 

     var getText = $(this).find(':selected').text();
     alert (getText); // show the text value of the selected element ...

 });