提交后保留选定的值 [英] Keep the selected value after submit

查看:80
本文介绍了提交后保留选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉菜单,我想在按下提交"按钮后保留选择的值. HTML在这里

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

<select name="pickupdatehour">
    <option label="00" selected="selected" value="00">00</option>
    <option label="01" value="1">01</option>
    <option label="02" value="2">02</option>
...
    <option label="23" value="23">23</option>
</select>

我没有为此使用常规的php,因为我在wordpress中创建了一个网站,并且我需要大量的代码片段来执行此操作,我在多个页面上都具有该选择框,并且我需要Java或jQuery脚本帮助我在每个页面上保留选定的值.

I'm not using regular php for this because i'm making a site in wordpress and i would need a ton of snippets to do this,i have this select box on multiple pages and i would need a Java or jQuery script to help me keep the selected value on each page.

推荐答案

如果您有 jQuery Cookie插件 ,您可以使用它在每次做出选择时将选择的值存储到cookie中,并且每次加载表单时都会检查是否设置了cookie,并使用cookie中的值

If you have the jQuery Cookie Plugin, you can use it to store the value of the select into cookie every time a selection is made and whenever the form loads it would check if the cookie is set and use the value in the cookie.

$(function() {
    var timeCookie = $.cookie( "timeCookie" ),
        selElem = $('select[name=pickupdatehour]');
    selElem.on('change', function() {
        $.cookie( "timeCookie", this.value );
    });
    if( timeCookie != undefined ) {
        selElem.val( timeCookie );
    } else {
        $.cookie( "timeCookie", selElem.val() );
    }
});

此处是 一个有效的演示.

HERE IS a working demo.

这篇关于提交后保留选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆