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条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用