having trouble adding html to with jQuery
I have a link on the page which when clicked opens up a thick-box window with Ajax modal login content. What is odd is that if I remove the alert('');
the next jquery line of code does not work. $('#logintest').before('<input type="hidden" value="' +st+ '" name="ReturnTo">');
If the alert('');
is there the hidden input is added to the thickbox modal window before the input with the id of "logintest" as it should.
I haven't been able to figure out why, anyone?
Here is the code on the main page
<a class="login_links" id="myaccount" href="/myaccount.asp">My Account</a>
<script type="text/javascript" language="javascript">
$(function(){
$(".login_links").click(function(){
var t = this.title || null;
var s = $(this).attr("href");
var g = this.rel || false;
var l = '/v/AjaxLogin.asp?height=100&width=300&modal=true'
tb_show(t,l,g);
var st = s.substr(s.lastIndex开发者_Go百科Of("/") + 1);
alert('');
$('#logintest').before('<input type="hidden" value="' +st+ '" name="ReturnTo">');
return false;
});
});
</script>
And here is the code on the ajax page
<div style="text-align:center ">
<form action="/login.asp" method="post" name="loginform">
<table border="0" cellpadding="3" cellspacing="3" style="margin:0 auto;">
<tr>
<td>Username:</td>
<td><input type="text" maxlength="75" size="30" value="" name="email"></td>
</tr>
<tr>
<td>Password:</td>
<td align="left"><input id="logintest" type="password" maxlength="20" size="20" name="password"></td>
</tr>
<tr align="right">
<td colspan="2"><input type="submit" value="Login"> <input type="submit" id="cancel" value="Cancel" onclick="tb_remove();return false;"></td>
</tr>
Ajax loading is asynchronous. The reason its not working without the alert is because the html from the ajax request hasnt been appended to the dom yet. You need to move that code to the success callback. Im not sure if TB provides for that or not.
精彩评论