dongtiaozhou4914 2012-08-20 21:15
浏览 52
已采纳

.bind()无法设置cookie

I have a function to set cookies, and i have .bind() set on some of my links so that they trigger the cookie function when clicked. Right now, when they are clicked no cookie is set. I've spent hours on this and cant figure it out.

<script type="text/javascript"> 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=/";
}
$("a#en").bind("click", function () {
    createCookie('TR_LNG', 'en', 90);
});
$("a#fr").bind("click", function () {
    createCookie('TR_LNG', 'fr', 90);
});
$("a#de").bind("click", function () {
    createCookie('TR_LNG', 'de', 90);
});
$("a#ja").bind("click", function () {
    createCookie('TR_LNG', 'ja', 90);
});
$("a#pl").bind("click", function () {
    createCookie('TR_LNG', 'pl', 90);
});
$("a#es").bind("click", function () {
    createCookie('TR_LNG', 'es', 90);
});
</script> 

HTML

<div id="langselectsplash" class="langselectsplash">
    <div id="select">
        <img src="http://mysite.org/wp-content/uploads/2012/08/sarvalogo2.png"
        />
        <p>
            <style>
            #tr_setdeflang {
                display:none;
            }

            </style>
            <div class="no_translate transposh_flags">
                <a id="en" href="/about/" class="tr_active">English</a>
                <a id="fr" href="/fr/about/">Français</a>
                <a id="de" href="/de/about/">Deutsch</a>
                <a id="ja" href="/ja/about/">日本語</a>
                <a id="pl" href="/pl/about/">Polski</a>
                <a id="es" href="/es/about/">Español</a>
            </div>
        </p>
    </div>
</div>
<div id="langsplashfade"></div>

The PHP code that outputs the above:

<?php
echo "<script type=\"text/javascript\"> 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=/\";}
";
foreach ($args as $langrecord) {
    echo "$(\"a#{$langrecord['isocode']}\").bind(\"click\", function() {createCookie('TR_LNG','{$langrecord['isocode']}',90);});
";
}
echo "</script>";
?>
   <?php
echo " <div id=\"langselectsplash\" class=\"langselectsplash\"><div id=\"select\"><img src=\"http://mysite.org/wp-content/uploads/2012/08/sarvalogo2.png\" /><p>";
?>
           <?php
echo "<style>#tr_setdeflang{display:none;}</style><div class=\"" . NO_TRANSLATE_CLASS . " transposh_flags\" >";
foreach ($args as $langrecord) {
    echo "<a id=\"{$langrecord['isocode']}\" href=\"{$langrecord['url']}\"" . ($langrecord['active'] ? ' class="tr_active"' : '') . '>' . "{$langrecord['langorig']}</a>";
}
echo "</div>";
?>
  • 写回答

2条回答 默认 最新

  • doubi2014 2012-08-20 21:22
    关注

    You need to put the code where you are binding events on the DOM ready event:

    $(document).ready(function(){
    //Your code
    });
    

    or

    $(function(){
    //Your code
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)