在jquery对话框中设置cookie,并在使用邮政编码提交后在页面上显示所选值 [英] set cookie in jquery dialog and show selected value on page after submit with postcode

查看:191
本文介绍了在jquery对话框中设置cookie,并在使用邮政编码提交后在页面上显示所选值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个页面,人们必须填写他们的邮政编码,以便他们可以搜索附近区域的工厂。

i'm making a page where people have to fill in their postcode so they can search factories in a near area.

我使用jquery模式对话框,必须输入他们的邮政编码,然后点击提交。然后我想要一个cookie设置,所以当用户第二次进入网站时,邮政编码仍然设置。所以他可以直接搜索工厂。

i used the jquery modal dialog where people have to enter their postcode and click submit. then i want a cookie to be set so when the user enters the site for a second time, the postcode is still set. so he can search for factories directly.

我有这个弹出窗口:

i have this popup:

用户必须填写他们的邮政编码,然后点击opslaan(save)

the people have to fill in their postcode there and then click opslaan (save)

在这里你可以看到mainpage的搜索功能。人们可以在不同的公里搜索。

Here you can see the mainpage with the search function. people can search on different kilometers.

我想要一个标签或填写邮政编码的东西,以便用户可以用他们的邮政编码进行搜索。

I want a label or something with the filled in postcode so people can search with their postcode.

编辑:

我的jquery对话框代码:

my jquery dialog code:

$(function() {

$( "#dialog" ).dialog(
{ 
show: "slow",
modal: "true",
width: 600,
show: "fold",
hide: "fade",
resizable: false,
draggable: false,
buttons: [ { id: "go", text: "Opslaan", click: function() { $( this ).dialog( "close" ); } } ],
open: function(event, ui) { $(".ui-dialog-titlebar-close").hide();  }});
$(".ui-widget-overlay").css({background: "#000", opacity: 0.8});
});

当我输入此代码时:

<input type="text" id="postcode" value="" name="search"/>
<button id="go">Opslaan</button>

在jQuery对话框的div中。它不工作。

In the div for the jquery dialog. it does not work.

<div id="dialog" title="Welkom bij OostWestRegioBest.nl">
  <p>Vul hier uw postcode in en druk op opslaan:</p>
  <br />


</div>


推荐答案

我建议使用jquery cookie: https://github.com/carhartl/jquery-cookie

I recommend jquery cookie here: https://github.com/carhartl/jquery-cookie

$.cookie('postcode', $('#postcode').val());

来设置它,并读取它:

if(typeof $.cookie('postcode') !== 'undefined'){
    $('#postcode').val($.cookie('postcode'));
}



例如,如果您有以下HTML:

So, for example, if you have the following HTML:

<input type="text" id="postcode">
<button id="go">Go!</button>

然后你的JS看起来像这样:

Then your JS would look like so:

$(document).ready(function(){
    //story the cookie on button press
    $('#go').click(function(){
            $.cookie('postcode', $('#postcode').val());        
    });

    //retrieve the cookie on load if it's not undefined
    if(typeof $.cookie('postcode') !== 'undefined'){
            $('#postcode').val($.cookie('postcode'));
    }
});

以下是一个功能示例: http://tinker.io/38617/4

Here is a functioning example: http://tinker.io/38617/4

这篇关于在jquery对话框中设置cookie,并在使用邮政编码提交后在页面上显示所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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