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?
我通过Jquery(ajax)加载一个面板(html),在面板中有一个带复选框的loginform。我想在用户点击复选框后在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>
1 个解决方案
#1
0
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
我相信这是因为你使用的是change()事件而不是live() - 因为假设#cookie只在你的Ajax事件之后被调用到页面中,它在初始渲染时不存在它无法绑定更改event - jquery API中的checkout live()
http://api.jquery.com/live/
UPDATE
UPDATE
Following your comments I've put together the following mock up in JSFiddle, which seems to work.
根据你的评论,我在JSFiddle中整理了以下模拟,这似乎有效。
http://jsfiddle.net/beardtwizzle/52JQX/2/
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)
我在测试期间遇到的唯一错误与$ .cookie有关 - 你需要包含一个插件(http://plugins.jquery.com/files/jquery.cookie.js.txt)
UPDATE 2
更新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.
问题肯定在于cookie设置 - 尝试发出警报('hello world');代替cookie代码你应该看到它到达那里 - 假设你正在使用live('change',function()...而不是bind。)
BUT, thats not your real problem - your real problem is that this method of 'remember me' is INSECURE on so many levels.
但是,这不是你真正的问题 - 你真正的问题是这种“记住我”的方法在很多层面都是不可靠的。
- You are attempting to store a poor visitors password in plain text - which is BAD
- 您试图以纯文本存储可怜的访问者密码 - 这是不好的
- 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)
- 当你在用户的机器上看到一个cookie时,你只需要写出你所在领域的价值。通过这样做,你假设用户有一个来自你网站的真正的cookie - 并且没有注入一些他们自己的恶意代码,请参阅(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).
我的建议是完全放弃这段代码,然后离开并阅读网络安全性,然后重新解决它。如果你继续这样做,你就会伤害无辜的用户(并严重损害你的声誉)。
#1
0
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
我相信这是因为你使用的是change()事件而不是live() - 因为假设#cookie只在你的Ajax事件之后被调用到页面中,它在初始渲染时不存在它无法绑定更改event - jquery API中的checkout live()
http://api.jquery.com/live/
UPDATE
UPDATE
Following your comments I've put together the following mock up in JSFiddle, which seems to work.
根据你的评论,我在JSFiddle中整理了以下模拟,这似乎有效。
http://jsfiddle.net/beardtwizzle/52JQX/2/
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)
我在测试期间遇到的唯一错误与$ .cookie有关 - 你需要包含一个插件(http://plugins.jquery.com/files/jquery.cookie.js.txt)
UPDATE 2
更新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.
问题肯定在于cookie设置 - 尝试发出警报('hello world');代替cookie代码你应该看到它到达那里 - 假设你正在使用live('change',function()...而不是bind。)
BUT, thats not your real problem - your real problem is that this method of 'remember me' is INSECURE on so many levels.
但是,这不是你真正的问题 - 你真正的问题是这种“记住我”的方法在很多层面都是不可靠的。
- You are attempting to store a poor visitors password in plain text - which is BAD
- 您试图以纯文本存储可怜的访问者密码 - 这是不好的
- 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)
- 当你在用户的机器上看到一个cookie时,你只需要写出你所在领域的价值。通过这样做,你假设用户有一个来自你网站的真正的cookie - 并且没有注入一些他们自己的恶意代码,请参阅(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).
我的建议是完全放弃这段代码,然后离开并阅读网络安全性,然后重新解决它。如果你继续这样做,你就会伤害无辜的用户(并严重损害你的声誉)。