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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题