使用 jQuery 添加 html 时遇到问题
我的页面上有一个链接,单击该链接会打开一个包含 Ajax 模式登录内容的厚框窗口。奇怪的是,如果我删除 alert('');
下一行 jquery 代码将不起作用。 $('#logintest').before('');
如果 alert ('');
是否隐藏输入被添加到厚盒模式窗口中,位于 id 为“logintest”的输入之前。
我一直无法弄清楚为什么,有人吗?
这是主页上的代码
<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.lastIndexOf("/") + 1);
alert('');
$('#logintest').before('<input type="hidden" value="' +st+ '" name="ReturnTo">');
return false;
});
});
</script>
这是ajax页面上的代码
<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>
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.lastIndexOf("/") + 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ajax 加载是异步的。没有警报它就无法工作的原因是因为 ajax 请求中的 html 尚未附加到 dom 中。您需要将该代码移至成功回调。我不确定结核病是否提供了这一点。
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.