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?

  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.

Live Demo

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);
  });
});

Try this little script, to check or creating cookies actually works ...

<?php
    if (!isset($_COOKIE['MyCookie'])) {
    setcookie ('MyCookie', 'yes', time() + 3600);
    echo "cookie is CREATED";
    }
    else{
    echo "cookie EXIST";
    }
?>