An alternative to exchange in jquery?

I am trying to fire an ajax event and pass the value of the list selection options as arguments in an ajax call. Unfortunately I am triggering a .change event call and it passes the select parameter values ​​before a new parameter is selected (e.g. passing previously selected parameter values). Is there an event that will receive the current values ​​of the selected parameter? Thank you very much in advance,

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

      

In JS:

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

      

I wanted to use .trigger, but I think it shoots in advance too.

+3


source to share


2 answers


I think .change () is what you want, but you are overusing it. The change event fires after a value change. 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 can also set values ​​in tags <option>

:

<option value="something"> Option 2</option>

      

+4


source


You must be doing something wrong when getting the current selection value. Works correctly for me.

http://jsfiddle.net/TJ2eS/



<select id="demo">
    <option value=""></option>
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
</select>

$("#demo").change(function() {
    alert("current select value " + $(this).val());
});

      

0


source







All Articles