最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to save DropDown selection with HTML5 storage? - Stack Overflow

programmeradmin10浏览0评论

I have;

<form method="post" action="search.php">
     <select name="country" class="dropdownselect" >

         <option value="">Select Country</option>   

             <option value="Afghanistan" selected="selected">Afghanistan</option>
             <option value="Albania" selected="selected">Albania</option>
             <option value="Algeria" selected="selected">Algeria</option>
     </select>
 </form> 

And then in search.php;

$country = $country; 
+
SQL query for $country.. 

I need to save DropDown selection to stay selected when someones preform search.

Can it be done with HTML5 storage func?

Thanks in advance

I have;

<form method="post" action="search.php">
     <select name="country" class="dropdownselect" >

         <option value="">Select Country</option>   

             <option value="Afghanistan" selected="selected">Afghanistan</option>
             <option value="Albania" selected="selected">Albania</option>
             <option value="Algeria" selected="selected">Algeria</option>
     </select>
 </form> 

And then in search.php;

$country = $country; 
+
SQL query for $country.. 

I need to save DropDown selection to stay selected when someones preform search.

Can it be done with HTML5 storage func?

Thanks in advance

Share edited Oct 8, 2012 at 20:04 Michal Klouda 14.5k7 gold badges56 silver badges79 bronze badges asked Oct 8, 2012 at 19:38 xulxul 2105 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Yes, you can store it on select change and then read on page load:

$(document).ready(function() {

    var item = window.localStorage.getItem('country');
    $('select[name=country]').val(item);

    $('select[name=country]').change(function() {
       window.localStorage.setItem('country', $(this).val());
    });

});

DEMO

发布评论

评论列表(0)

  1. 暂无评论