对通过 Ajax 加载的内容应用 jquery 函数
我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这是因为您使用的是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()... 而不是绑定。但是,这不是你真正的问题 - 你真正的问题是这种“记住我”的方法在很多层面上都是不安全的。
根据上述内容我的答案
我的建议完全删除此代码,然后离开并阅读有关网络安全的内容,然后重新解决它。如果您继续这样做,您将伤害无辜的用户(并严重损害您的声誉)。
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.
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).