Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect with JQuery when a select changes

I have a Jqgrid which dynamically generates selects like this:

    <select id="won" style="width: 65px;">
       <option value="">-WON?</option>
       <option value="1" selected>Bet1</option>
       <option value="2" >Bet2</option>
       <option value="3" >Bet3</option>
    </select>

Each one have a different selected option. I would like to detect when one select changes so I can save it in my database.

I'm trying with:

         $('#won').change(function(){
               alert("PROBANDO");
          });

But is not working at all.

like image 375
user2876368 Avatar asked Oct 22 '13 21:10

user2876368


People also ask

How to detect select change jQuery?

$(this). find("option:selected"). attr('value') tells you the new "won value" if it is what you are looking for ...

Which event can be used to detect changes in list jQuery?

The change() is an inbuilt method in jQuery that is used to detect the change in value of input fields.

How do I select a specific Dropdownlist using jQuery?

Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).

How can I change the selected value of a Dropdownlist using jQuery?

Using the jQuery change() method; you can set the selected value of dropdown in jquery by using id, name, class, and tag with selected html elements; see the following example for that: Example 1 :- Set selected value of dropdown in jquery by id.


2 Answers

The problem is that the select are created dynamically, then you need to use .on()

try this:

         $(document).on('change','#won',function(){
               alert("PROBANDO");
          });
like image 83
Alessandro Minoccheri Avatar answered Oct 18 '22 20:10

Alessandro Minoccheri


This should work, be sure you did not forget $(document).ready(function() {

$(document).ready(function() {  
    $('#won').change(function(){
        alert( $(this).find("option:selected").attr('value') );       
    });
 });
like image 36
Corinne Kubler Avatar answered Oct 18 '22 21:10

Corinne Kubler