ChatGPT解决这个技术问题 Extra ChatGPT

jQuery select change event get selected option

I bound an event on the change event of my select elements with this:

$('select').on('change', '', function (e) {

});

How can I access the element which got selected when the change event occurs?

@superuberduper I avoided jquery for as long as I could but resistance is futile. You'll feel so much better after you've been assimilated.

N
Naftali
$('select').on('change', function (e) {
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    ....
});

What does the $("selector", this) syntax mean? I have a general idea, but I'm not totally sure
@JoshWillik with $("selector", this) you are finding all selector elements inside this's context. Writing $("selector", this) is almost the same than $(this).find('selector')
@JoshWillik: since $() is an alias of jQuery(), you can find the documentation here: api.jquery.com/jQuery The signature stated there is obviously jQuery( selector [, context ] ). @Bellash: if it's "almost the same", what is the difference? Or what is faster? I prefer .find() since this is more OO IMO...
How to check in case of multiple select?
@AdrianFöder For you and other people looking for it, .find() is around 10% faster according to this answer: stackoverflow.com/a/9046288/2767703
B
Bellash

You can use the jQuery find method

 $('select').change(function () {
     var optionSelected = $(this).find("option:selected");
     var valueSelected  = optionSelected.val();
     var textSelected   = optionSelected.text();
 });

The above solution works perfectly but I choose to add the following code for them willing to get the clicked option. It allows you get the selected option even when this select value has not changed. (Tested with Mozilla only)

    $('select').find('option').click(function () {
     var optionSelected = $(this);
     var valueSelected  = optionSelected.val();
     var textSelected   = optionSelected.text();
   });

Binding to option events is a risky proposition, particularly on mobile devices. For example, Webkit doesn't support this event correctly: bugs.chromium.org/p/chromium/issues/detail?id=5284
Ok cool! As I said, the second solution is not supported in many browsers!
C
Community

Delegated Alternative

In case anyone is using the delegated approach for their listener, use e.target (it will refer to the select element).

$('#myform').on('change', 'select', function (e) {
    var val = $(e.target).val();
    var text = $(e.target).find("option:selected").text(); //only time the find is required
    var name = $(e.target).attr('name');
}

JSFiddle Demo


+1 because this is what i want, but when i tried this locally, its not working .Only work at jsfiddle, what are the CDN do i have to add?
佚名
<select id="selectId">
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
</select>


$('#selectId').on('change', function () {
     var selectVal = $("#selectId option:selected").val();
});

First create a select option. After that using jquery you can get current selected value when user change select option value.


can you please share more detail explanation of your answer ?
@MostafizurRahman my code very easy that's why i wrote only code.
simple and fewer code thanks, I'm looking for this answer.
u
user229044

I find this shorter and cleaner. Besides, you can iterate through selected items if there are more than one;

$('select').on('change', function () {
     var selectedValue = this.selectedOptions[0].value;
     var selectedText  = this.selectedOptions[0].text;
});

selectedOptions is not available in all browsers.
@BernhardDöbler which ones? any source?
I copied your code to IE 11 and got an error. I ended up with this.options[ this.options.selectedIndex ]. Mozilla says is supported by Firefox from 26, IE No support.
It is supported by all browsers except IE. developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/…
S
Serdar Karaca
$('#_SelectID').change(function () {
        var SelectedText = $('option:selected',this).text();
        var SelectedValue = $('option:selected',this).val();
});

R
Recep Can

Another and short way to get value of selected value,

$('#selectElement').on('change',function(){
   var selectedVal = $(this).val();
                       ^^^^  ^^^^ -> presents #selectElement selected value 
                         |
                         v
               presents #selectElement, 
});

if this is the case, why wont "var selectedVal=$("#selectElement").val()" work¿
The case is getting value when change event emitted on select element. Value wouldn't be updated when select changes if you do like that.
m
mickySimons

This can also work fine

(function(jQuery) { $(document).ready(function() { $("#select_menu").change(function() { var selectedOption = $("#select_menu").val() }); }); })(jQuery);


a
alex Roosso

See official API documentation https://api.jquery.com/selected-selector/

This good works:

$( "select" ).on('change',function() {
  var str = "";
  // For multiple choice
  $( "select option:selected" ).each(function() {
    str += $( this ).val() + " "; 
  });
});

and

$( "select" ).on('change',function() {
  // For unique choice
  var selVal = $( "select option:selected" ).val(); 
});

and be easy for unique choice

var SelVal = $( "#idSelect option:selected" ).val();

D
Developer

You can use this jquery select change event for get selected option value

For Demo

$(document).ready(function () { $('body').on('change','#select', function() { $('#show_selected').val(this.value); }); }); Learn Jquery value Method