Apply jquery function on content loaded via Ajax
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="s开发者_开发问答ubmit" name="submit" value="Login" />
</form>
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.
- 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)
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).
精彩评论