Jump to content
Sign in to follow this  
Gann

Хочу сделать простенький выбор города, поможете?

Recommended Posts

Здравствуйте!

 

Как можно сюда:

 

$("#selectItem").change(function(){
    $('.containerss').find('div').hide();
    var selected = $('#selectItem option:selected').attr('id');
    $('.' + selected).show();
});  

 

добавить в LocalStorage выбранный select option?

 

Полный код у меня такой:

 

 

<style>.containerss div {display: none;}</style>
<select id="selectItem">
<option id="chelyabinsk">Челябинск</option>
<option id="moskva">Москва</option>
<option id="piter">Санкт-Петербург</option>
<option id="nnovgor">Нижний Новгород</option>
<option id="ekat">Екатеринбург</option>
</select>

<div class="containerss">

<div class="chelyabinsk" style="display: block;"></div>

<div class="moskva"></div>
<div class="piter"></div>
<div class="nnovgor"></div>

<div class="ekat"></div>
</div>
<script type="text/javascript">
<!--
$("#selectItem").change(function(){
    $('.containerss').find('div').hide();
    var selected = $('#selectItem option:selected').attr('id');
    $('.' + selected).show();
});  
-->

</script>

Share this post


Link to post
Share on other sites
var selected = $('#selectItem option:selected').attr('id');
localStorage.setItem("myKey", selected);

 

  • +1 2

Share this post


Link to post
Share on other sites
41 минуту назад, crapic сказал:
var selected = $('#selectItem option:selected').attr('id');
localStorage.setItem("myKey", selected);

 

Спасибо!

 

Строчку добавил. 

Теперь может быть поможете, как взять сохранённое значение (того селекта, который выбрали) их localStorage и вывести его? Сейчас выбранное значение селекта после перезагрузки страницы не сохраняется.

Edited by Gann

Share this post


Link to post
Share on other sites
$( function() {
if (localStorage.getItem("myKey")) {

    var stored_select = localStorage.getItem("myKey");

    $('#' + stored_select).prop( "selected", true );
}

});

 

  • +1 2

Share this post


Link to post
Share on other sites
7 минут назад, crapic сказал:
$( function() {
if (localStorage.getItem("myKey")) {

    var stored_select = localStorage.getItem("myKey");

    $('#' + stored_select).prop( "selected", true );
}

});

 

 

Ещё раз спасибо!

Share this post


Link to post
Share on other sites

Вставил в скрипт новую функцию - не работает... видимо не туда вставил..

 

<script type="text/javascript">
<!--

$( function() {
var selected = $('#selectItem option:selected').attr('id');
    localStorage.setItem("myKey", selected);
if (localStorage.getItem("myKey")) {

    var stored_select = localStorage.getItem("myKey");

    $('#' + stored_select).prop( "selected", true );
}

});

$("#selectItem").change(function(){
    $('.containerss').find('div').hide();
    var selected = $('#selectItem option:selected').attr('id');
    localStorage.setItem("myKey", selected);
    $('.' + selected).show();
});  
-->
</script>

 

 

Помогите разобраться, пожалуйста.

 

Edited by Gann

Share this post


Link to post
Share on other sites

вот рабочий для jquery > 1.6

 

<style>.containerss div {display: none;}</style>
<select id="selectItem">
<option id="chelyabinsk">Челябинск</option>
<option id="moskva">Москва</option>
<option id="piter">Санкт-Петербург</option>
<option id="nnovgor">Нижний Новгород</option>
<option id="ekat">Екатеринбург</option>
</select>
<div class="containerss">
<div class="chelyabinsk" style="display: block;"></div>
<div class="moskva">Это москва</div>
<div class="piter">а это питер</div>
<div class="nnovgor"> а вот он нижний</div>
<div class="ekat">вот ты какой ебург</div>
</div>
<script type="text/javascript">
<!--
$( function() {

if (localStorage.getItem("myKey")) {
    var stored_select = localStorage.getItem("myKey");
    $('#' + stored_select).prop( "selected", true );
    $('.' + stored_select).show();
    }
});
$("#selectItem").change(function(){
    $('.containerss').find('div').hide();
    var selected = $('#selectItem option:selected').attr('id');
    localStorage.setItem("myKey", selected);
    $('.' + selected).show();
});  
-->
</script>

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.