对通过 Ajax 加载的内容应用 jquery 函数

发布于 2024-11-08 15:34:59 字数 1437 浏览 0 评论 0原文

我通过 Jquery (ajax) 加载面板 (html),面板中有一个带有复选框的登录表单。我想在用户单击复选框后在 cookie 中设置用户名(记住我)。它不起作用。有解决办法吗?

$('#cookie').bind('change', function() {
       $.cookie("log_user", $("#log_user").val(), {expires: 14});
       $.cookie("log_pass", $("#log_pass").val(), {expires: 14});
});

  $("#gate").click(function () {
    var panel = $("#panel");
    if (!panel.data("loaded")) {
        $("#panel").load("/v3/ajax/panel.php");
        panel.data("loaded", true);

        var log_user = $.cookie('log_user');
        var log_pass = $.cookie('log_pass');
        // autofill the fields
        $('#log_user').attr("value", log_user);
        $('#log_pass').attr("value", log_pass);

    }
    panel.slideToggle("slow");
});




<form action="/members/login.php" method="post">
<label for="log_user">Username</label><input id="log_user" type="text" name="user" value="" maxlength="50"  /><br />
<label for="log_pass">Password  </label><input id="log_pass" type="password" name="pass" value=""  maxlength="50"  /><br />
<input id="cookie" type="checkbox" name="cookie" value="do" style="border: 0px;" /><label for="cookie"><small>Remember me</small></label><br />

<a title="Join Sionvalais" href="/members/register.php">register</a><br />
<input type="submit" name="submit" value="Login"  />
</form>

I load a panel (html) via Jquery (ajax), in the panel there is a loginform with a checkbox. I want to set the username in a cookie once the user clicks the checkbox (remember me). It is not working. Is there a solution?

$('#cookie').bind('change', function() {
       $.cookie("log_user", $("#log_user").val(), {expires: 14});
       $.cookie("log_pass", $("#log_pass").val(), {expires: 14});
});

  $("#gate").click(function () {
    var panel = $("#panel");
    if (!panel.data("loaded")) {
        $("#panel").load("/v3/ajax/panel.php");
        panel.data("loaded", true);

        var log_user = $.cookie('log_user');
        var log_pass = $.cookie('log_pass');
        // autofill the fields
        $('#log_user').attr("value", log_user);
        $('#log_pass').attr("value", log_pass);

    }
    panel.slideToggle("slow");
});




<form action="/members/login.php" method="post">
<label for="log_user">Username</label><input id="log_user" type="text" name="user" value="" maxlength="50"  /><br />
<label for="log_pass">Password  </label><input id="log_pass" type="password" name="pass" value=""  maxlength="50"  /><br />
<input id="cookie" type="checkbox" name="cookie" value="do" style="border: 0px;" /><label for="cookie"><small>Remember me</small></label><br />

<a title="Join Sionvalais" href="/members/register.php">register</a><br />
<input type="submit" name="submit" value="Login"  />
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2024-11-15 15:34:59

我相信这是因为您使用的是change()事件而不是live() - 因为,假设#cookie仅在Ajax事件之后被调用到页面中,它在初始渲染时不存在,它无法绑定更改事件 - 在 jquery API

http://api.jquery.com/live/

中签出 live() 更新

根据您的评论,我在 JSFiddle 中整理了以下模型,这似乎有效。

http://jsfiddle.net/beardtwizzle/52JQX/2/

我在过程中遇到的唯一错误我的测试与 $.cookie 有关 - 您需要包含一个插件(http://plugins.jquery.com/files/jquery.cookie.js.txt)

更新2

问题肯定似乎取决于 cookie 设置 - 尝试将 alert('hello world'); 代替 cookie 代码,您应该看到它到达那里 - 假设您正在使用 live ('change',function()... 而不是绑定。

但是,这不是你真正的问题 - 你真正的问题是这种“记住我”的方法在很多层面上都是不安全的。

  1. 你正试图存储一个可怜的访问者纯文本的密码 - 这是不好的
  2. 当您在用户的计算机上看到 cookie 时,您只需将其中的任何内容写到您的字段的值属性中即可。来自您网站的 cookie - 并且没有注入一些他们自己的恶意代码,请参阅(http://en.wikipedia.org/wiki/Cross-site_scripting)

根据上述内容我的答案

我的建议完全删除此代码,然后离开并阅读有关网络安全的内容,然后重新解决它。如果您继续这样做,您将伤害无辜的用户(并严重损害您的声誉)。

I believe this is because you're using change() event rather than live() - as, assuming #cookie is only called into the page after your Ajax event, it doesn't exist at initial rendertime it can't bind the change event - checkout live() in the jquery API

http://api.jquery.com/live/

UPDATE

Following your comments I've put together the following mock up in JSFiddle, which seems to work.

http://jsfiddle.net/beardtwizzle/52JQX/2/

The only error I got during my tests were related to $.cookie - for which you need to include a plugin (http://plugins.jquery.com/files/jquery.cookie.js.txt)

UPDATE 2

The problem definitely seems to be down to the cookie setting - try putting an alert('hello world'); in-place of the cookie code and you should see that its getting there - that's assuming you're using live('change',function()... rather than bind.

BUT, thats not your real problem - your real problem is that this method of 'remember me' is INSECURE on so many levels.

  1. You are attempting to store a poor visitors password in plain text - which is BAD
  2. When you see a cookie on a user's machine you're going to just write out whatever is in there to the value attibute of your fields. By doing this you're assuming that the user has a genuine cookie from your site - and hasn't instead injected some of their own malicious code see (http://en.wikipedia.org/wiki/Cross-site_scripting)

MY ANSWER in light of the above

My suggestion would be to drop this code ENTIRELY, then go away and read up on web security, then re-address it. If you go ahead and get this working you're going to hurt innocent users (and damage your reputation badly).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文