vendredi 29 mai 2015

jQuery check if select field changed

I would like to make a jQuery check if a select field was changed. If it changes, set an alert message (has changed), if returned to default, set another alert message (default value).

$('select').on('change',function () {
    var isDirty = false;

    $('select').each(function () { 
        var $e = $(this);      
        if (!$e[0].options[$e[0].selectedIndex].defaultSelected) {
        isDirty = true;
        }    
    });

    if(isDirty == true) { 
        alert("has changed");
    } else {
        alert("default value");          
    }
});

Please advise if this is the proper way.

Aucun commentaire:

Enregistrer un commentaire