Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to .change in jquery?

I'm trying to fire an ajax event, and passing the value of select list options as arguments in the ajax call. Unfortunately, I'm firing the call on the .change event, and it is passing the values of the select option before the new option has been selected (i.e passing the previously selected options values). Is there an event which will get the current values of the option selected? Much thanks in advance,

<select id='theList'>
   <option> Option 1</option>
   <option> Option 2</option>
</select>

In the JS:

$('#theList').change( function() {
$.getJSON('Home/MethodName', { var1: Option1Value, var2: MiscValue}, function(data) {
      //Execute instructions here
}
)});

I wanted to use .trigger, but I think that fires beforehand as well.

like image 700
Tui Popenoe Avatar asked Jan 29 '26 02:01

Tui Popenoe


1 Answers

I think .change() is what you want, but you're misusing it. The change event fires after the value has changed. In your handler, you need to read the new value:

$('#theList').change( function() {
  var value = $('#theList').val();
  $.getJSON('Home/MethodName', { your_key: value }, function(data) {
      // ...
  }
)});

You also might want to set values on your <option> tags:

<option value="something"> Option 2</option>
like image 107
kevingessner Avatar answered Jan 31 '26 14:01

kevingessner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!