With

$('select').on('change', function() { var val = $(this).val(); // Показывает значение выбранное сейчас }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="name"> <option value="1" selected>Мото</selected> <option value="2">Авто</selected> </select> 

And if I had chosen: "moto" before the choice, how can this be determined in the context of the change event?

  • 2
    write old value to separate variable - Cheg
  • @Cheg and not to catch it through events? - user190134
  • through the event and record - Cheg

2 answers 2

We will use the focus event for the first save and then just update:

 (function(){ let previous; $("select").on('focus', e => previous = e.target.value).change(e => { // Прошлое значение value console.info(previous); // Необходимо обновить, иначе последующие смены не будут работать previous = e.target.value; }); }()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="name"> <option value="1" selected>Мото</option> <option value="2">Авто</option> </select> 

     $(function() { $('select').each(function() { $(this).data('old_val', $(this).val()); }); $('select').on('change', function() { var old = $(this).data('old_val'); // Старое значение берём var val = $(this).val(); // Показывает значение выбранное сейчас alert('Было ' + old + ' стало ' + val); // ...... $(this).data('old_val', $(this).val()); // Старое значение обновляем }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="name"> <option value="1" selected>Мото</selected> <option value="2">Авто</selected> </select>