将新内容加载到中并保留前一个中的输入字段值状态?
我正在尝试制作自己的 tumblr 搜索(仅限标签),因为他们自己的搜索功能实际上不起作用。
所以我快到了。它有效,但我需要搜索字段来保留用户搜索内容的值。
因此,如果我搜索“foobar”,它会将 tumblr.com/tagged/foobar 直接加载到我的 tumblr.com 中,但搜索字段仍然包含术语“foobar”
这是我的工作代码例外对于我试图将前一个搜索词插入到新加载的代码中,
提前致谢。
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
jQuery(function($) {
$('#syndex').keydown(function(event) {
if (event.keyCode == '13') {
var syndex = $('input:text').val();
$('body').load("http://syndex.me/tagged/"+ syndex);
$(this).val(syndex); //this is what's not working
}
});
});
</script>
</head>
<body>
<div id="logo">
<input id="syndex" type="text"/>
</div>
</body>
</html>
I'm trying to make my own tumblr search (tags only) since their own search function doesn't actually work.
So I'm nearly there. It works, but I need the search field to keep the value of what the user searched for.
So if I search for "foobar", it loads the tumblr.com/tagged/foobar directly into my tumblr.com, but the search field still contains the term "foobar"
Here is my working code except for the code where I'm trying to insert the previous search term into the newly loaded
Thanks in advance.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
jQuery(function($) {
$('#syndex').keydown(function(event) {
if (event.keyCode == '13') {
var syndex = $('input:text').val();
$('body').load("http://syndex.me/tagged/"+ syndex);
$(this).val(syndex); //this is what's not working
}
});
});
</script>
</head>
<body>
<div id="logo">
<input id="syndex" type="text"/>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将其放入
complete
函数中,该函数可以作为参数传递给load
。http://api.jquery.com/load/
Load
使AJAX 调用是异步的。您试图在加载内容之前设置该值。You need to put this into the
complete
function which can be passed as a parameter toload
.http://api.jquery.com/load/
Load
makes an AJAX call which is asynchronous. You are trying to set the value prior to the content being loaded.