You must save the select option to Storage . And also do ignoring the save on a specific name .

Code:

 var selects = $("#dialog_id select"); for (var key in selects) { if (!isNaN(parseFloat(key)) && isFinite(key)) { var select = selects[key]; var nameselect = $(select).attr('name'); $(select).val(localStorage.getItem(nameselect)); } } 

Actually thanks to it, in the dialog all selects are taken and when option selected in it, this choice is stored in Storage . The next time you open such a window, the last choice will be the default.

So, there are select to which such functionality should not apply. These individual select have given name .

Can I somehow exclude them? I would be very grateful for the advice!

  • Let name = "ignor" be an example - Denis
  • in the code there is no saving in localStorage , there is only reading from it by name - Vasily Barbashev
  • console.log ('selects:', selects); do you mean it? I apologize, I just started studying the storage. (( - Denis
  • `$ (select) .val (localStorage.getItem (nameselect));` Take data, take it, do not write it down. - Vasily Barbashev
  • In my case, this was enough for the idea ... work out and display the last choice. Although you have now driven me right into a corkscrew ... - Denis

1 answer 1

Sketched a code that saves the selected data in selects by name.

The code is working. Checked

 <div id="dialog_id"> <select name="vlad" onChange="selectChanges(this, value);"> <option value="1">Value1</option> <option value="2">Value2</option> <option value="3">Value3</option> </select> <select name="semen" onChange="selectChanges(this, value);"> <option value="1">Value1</option> <option value="2">Value2</option> <option value="3">Value3</option> </select> <select name="vasya" onChange="selectChanges(this, value);"> <option value="1">Value1</option> <option value="2">Value2</option> <option value="3">Value3</option> </select> </div> <script type="text/javascript"> var selects = $("#dialog_id select"); for (var key in selects) { if (!isNaN(parseFloat(key)) && isFinite(key)) { var select = selects[key]; var nameSelect = $(select).attr('name'); console.log(nameSelect); $(select).val(localStorage.getItem(nameSelect)); } } function selectChanges(obj, value) { var $obj = $(obj); var name = $obj.attr('name'); // дальше код проверки - нужно ли сохранять var notSaveIn = ['массив', 'ненужных', 'элементов']; if (notSaveIn.indexOf(name) === -1) { localStorage.setItem(name, value) console.log(localStorage.getItem(name)); } } </script> 
  • and if, for example, initially in onChange = "selectChanges (this, value);"? They are all generated by one script. Actually my problem is that it is necessary to exclude three selekta from such an ability as saving the choice. In this case, if I understood correctly, those selects are saved where onChange = "selectChanges (this, value);" is written, right? - Denis
  • @ Denis Added a code with a label // дальше код проверки - нужно ли сохранять - Vasily Barbashev