douxing1850 2014-03-21 07:21
浏览 61
已采纳

javascript中的Cookie无法使用live.click功能

I want to get the value of one text box if a particular button is clicked. I can't send the value through url so I tried using javascript. Here is my php code:

<a href="yahoo/Connected.php">
<input type='button' class='button' id='yahoo' value='Yahoo'></a>
<p>Your name, emails will be sent with your name in Subject</p>
<input type='text' class='inviteename' id='text' name='name'>

Here is my javascript code:

<script>
$('#yahoo').live('click',function() {
var inviteename = $('.inviteename').val();
function createCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}
createCookie('invitee_name',+inviteename,'1');
});
</script>

Through this, cookie is not being create, can anyone spot what wrong I'm doing?

  • 写回答

2条回答 默认 最新

  • drsh30452 2014-03-21 07:27
    关注
    1. your html is invalid. Why wrap input fields in a link?

    2. .live is deprecated, you should use .on - I use on submit here.

    3. you should use a cookie jQuery plugin instead of wrapping a named function into your click.

    <kbd>Live Demo</kbd>

    Please note the demo is using "p" instead of "name" to actualy do something. Hit F12, Run the demo once then reload to see the cookie was set

    <form id='yahoo'  action="yahoo/Connected.php">
    <input type='submit' class='button' value='Yahoo'></a>
    <p>Your name, emails will be sent with your name in Subject</p>
    <input type='text' class='inviteename' id='text' name='name'>
    </form>
    

    using

    $(function() {
      $('#yahoo').on('submit',function(e) {
        var inviteename = $('.inviteename').val();
        $.cookie('invitee_name',inviteename);
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)