如何在提交时将设置的cookie显示到另一个html页面 [英] how to display the set cookie to another html page on submit

查看:670
本文介绍了如何在提交时将设置的cookie显示到另一个html页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用jquery cookies创建html时。我已经知道如何设置,获取和删除cookie。一件事我不知道是如何显示设置的cookie在下一页。
i有这个html ccodes:

im practicing on creating a html using jquery cookies. i already know how to set, get, and delete cookies. one thing i don't know is how to display the set cookies on the next page. i have this html ccodes:

    <FORM id = 'login' meth0d = 'post' action = 'Coutput.html' >            
        <input id = 'name' type = "text"  value = ''>           
        <input id="btnSet" type="submit" value="Go"/>
        <input id="btndelete" type="button" value="delete cookie"/>
    </form>

我有这个JavaScript代码:

and i have this javascript code:

    $('form#login :submit').addClass('inputSubmitBtn').click(function(){
       if($('#name').val() == "" ){         
          $("#name").focus();
          alert('please input name');
          return false;
       }
       else{
          $.cookie("cookie_name", null);
          $.cookie('cookie_name', $('#name').val());
          alert('new cookie name' +$('#name').val()));
          $('#name').append($.cookie('cookie_name'));
       };           
    });

问题:


  1. 为什么每次我使用 $。cookie('cookie_name',$('#name')。val(),{expires:null,path:'/ domain:'cegi1.html',secure:true}); ,我的cookie总是设置为null,虽然我设置了另一个,它总是返回null?

  1. why is it that everytime i used $.cookie('cookie_name', $('#name').val(), { expires: null, path: '/', domain: 'cegi1.html', secure: true });, my cookie os always set to null, though i set another one, it always returns null?

如何在Coutput.html中显示我新设置的Cookie? (例如name = luke,Coutput.html中的输出必须为welcome luke)

how can i display my newly set cookie in Coutput.html? (ex. name = luke, output in Coutput.html must be "welcome luke")

请帮助..

推荐答案

看起来< input id =btnSettype =submit value =Go/> 在评估 $('form#login:submit')之前将表单提交到其他页面。 c $ c>还有你在 alert('new cookie name'+ $('#name')。val())); 这应该可以工作。

It seems that <input id="btnSet" type="submit" value="Go"/> is submitting form to the other page before evaluating $('form#login :submit').click(function(). And also you have got error on alert('new cookie name' +$('#name').val()));. This should work.

<script src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script>
$(function ()
{
    $("#btnSet").click(function(){
       if($('#name').val() == "" ){         
          $("#name").focus();
          alert('please input name');
          return false;
       }
       else{
          $.cookie("cookie_name", null);
          $.cookie('cookie_name', $('#name').val());
          alert('new cookie name' +$('#name').val());
          $('#name').append($.cookie('cookie_name'));
       };           
        window.location = $(this).parent('form').attr("action");
    });
})

</script>

 <form id = 'login' meth0d = 'post' action = 'Coutput.html' >            
        <input id ="name" type = "text"  value="">           
        <input id="btnSet" type="button" value="Go"/>
        <input id="btndelete" type="button" value="delete cookie"/>
 </form>

在我的 Coutput.html / p>

On my Coutput.html i have

<script src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script>
    alert($.cookie("cookie_name") );
</script>

如果要在窗口关闭时删除cookie,请使用窗口。 unload 事件和on jquery perhap 。您可以通过将Cookie重置为null来删除Cookie。

If you want to delete the cookie on window close, then use window.unload event and on jquery perhap this. And you can delete cookie by resetting it to null.

这篇关于如何在提交时将设置的cookie显示到另一个html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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