Keep the selected value after submit

前端 未结 2 1188
迷失自我
迷失自我 2021-01-21 09:56

I have a time drop down selection and i want to keep the selected value after the submit button has been pressed. Html here


                        
    
提交评论

  • 2021-01-21 10:28

    As mentioned in the comment above unless you're storing the value in a database and pulling that out each time your best bet would be either a cookie or localstorage. Personally I've found localstorage to be easier to work with and unless you need IE7 support you should be fine to use that.

    http://caniuse.com/#search=localstorage

    You could try something like this (untested):

    // On submit
    var pickupdatehour = $('#pickupdatehour').val()
    localStorage.setItem('storedPickup', pickupdatehour);
    
    // On the pages that have the select box
    jQuery(document).ready(function () {
      var loadedPickup = JSON.parse(localStorage.getItem('storedPickup'));
      $('#pickupdatehour').val(loadedPickup);
    });
    

    Edit: Sorry missed that it was name not ID in your select box. Use the select[name=pickupdatehour] instead of the #pickupdatehour as a selector.

    0 讨论(0)
  • 提交回复
    热议问题