久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

頁面刷新后jQuery cookie設置選擇下拉值

jQuery cookies setting select drop down value after page refresh(頁面刷新后jQuery cookie設置選擇下拉值)
本文介紹了頁面刷新后jQuery cookie設置選擇下拉值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

老實說,今天走到這一步后,我的大腦有點炸了.

In all honesty my brain is rather fried after getting this far today.

我正在嘗試使用此插件保存頁面上多個選擇下拉菜單的狀態:

I am trying to save the state of multiple select drop downs on page using this plugin:

http://plugins.jquery.com/project/cookies

我正在使用這個 jQuery 根據 ID 為不同的標題下拉菜單設置 cookie:

I am using this jQuery to set cookies for the different title drop downs based on their ID:

$(document).ready(function() {

// hide 'Other' inputs to start
$('.jOther').hide();

// event listener on all select drop downs with class of jTitle 
$(".jTitle").change(function(){

    //set the select value
    var val = $(this).val();

    if(val != "Other") {
        //$(this).nextAll('.jOther').hide();
        $(this).parent().find(".jOther").hide();
    } else {
        //$(this).nextAll('.jOther').show();
        $(this).parent().find(".jOther").show();
    }

    // Sets a cookie with named after the title field's ID attribute 
    $(this).cookify();

});

$(".jTitle").each(function(){

    // get the id of each Title select drop down
    var $titleId = $(this).attr('id');

    // get the value of the cookie for each cookie created above in $(this).cookify()
    var $cookieValue = $.cookies.get($titleId);

    // if value is 'Other' make sure it is shown on page refresh
    if ($cookieValue == 'Other') {

        // Show the other input
        $(this).parent().find(".jOther").show();

        // set select value to 'Other'
        $(this).val('Other');

    } else {

        // set to whatever is in the cookie
        $(this).val($cookieValue);

    }                       

}); 

});

發生的情況是,當沒有設置 cookie 時,選擇下拉菜單顯示一個空白選項,而我希望它默認為請選擇".

What is happening is that when no cookies are set the select drop down is displaying a blank option when i want it to default to 'Please select'.

我正在使用的 HTML 示例:

Sample HTML that i am using:

<td>
<select id="titleDepend1" class="inlineSpace jTitle">
    <option value="Please select">Please select...</option>
    <option value="Mr">Mr</option>
    <option value="Mrs">Mrs</option>
    <option value="Ms">Ms</option>
    <option value="Miss">Miss</option>
    <option value="Dr">Dr</option>
    <option value="Other">Other</option>
</select>
<label for="otherDepend1" class="inlineSpace jOther">Other</label>
<input type="text" class="text jOther" name="otherDepend1" id="otherDepend1" maxlength="6" />

因此,如果這是用戶第一次進入頁面并且他們沒有點擊任何下拉菜單,那么第一個值將為 null 而不是請選擇".

So if it is the first time the user is on page and they have not clicked any drop downs the first value will be null rather than 'Please select'.

推薦答案

我會更改這部分,如果 cookie 不存在,請不要弄亂下拉列表:

I'd change this portion, if the cookie isn't there, just don't mess with the dropdown:

$(".jTitle").each(function(){
  var $titleId = $(this).attr('id');
  var $cookieValue = $.cookies.get($titleId);
  if ($cookieValue == 'Other') {
    $(this).parent().find(".jOther").show();
    $(this).val('Other');
  } else if($cookieValue) {
    $(this).val($cookieValue);
  }                       
});

唯一的變化是在最后添加一個if檢查,看看是否有cookie...如果沒有,下拉菜單中的默認位置0(瀏覽器默認)將保持不變.

The only change is to add an if check on the end, see if there is a cookie...if not the default position of 0 in the dropdown (browser default) will be left alone.

這篇關于頁面刷新后jQuery cookie設置選擇下拉值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 精品九九| 欧美日韩久久精品 | 午夜影院官网 | 在线欧美激情 | 狠狠躁躁夜夜躁波多野结依 | 亚洲精品麻豆 | 亚洲精品一区二区三区在线 | 国产综合视频 | 国产精品日韩欧美一区二区 | 日韩福利| 视频在线一区 | 国产精品a一区二区三区网址 | 亚洲成人免费 | 91精品一区二区三区久久久久 | 日韩一区二区三区在线观看 | 欧美成人a∨高清免费观看 欧美日韩中 | 在线国产一区二区 | 精品久久网 | 欧美一区二区三区国产精品 | 国产成人精品一区二区三区在线 | 精品免费国产一区二区三区四区 | 国产高清视频在线观看 | 羞羞的视频在线观看 | 欧美日韩黄色一级片 | 欧美日韩专区 | 国产日产精品一区二区三区四区 | 久草热在线 | 四虎网站在线观看 | 国产精品视频导航 | a视频在线| 激情五月婷婷综合 | 亚洲精品久久久久国产 | 欧美一级三级在线观看 | av免费网站在线 | 免费国产视频在线观看 | www精品美女久久久tv | 欧美日韩视频在线 | 中文av网站 | 中文字幕视频三区 | 久久国产精品网 | 男人影音 |