【问题标题】:Loading new content into <body> and keeping input fields value from previous <body> state?将新内容加载到 <body> 并保持先前​​ <body> 状态的输入字段值?
【发布时间】:2011-09-30 20:21:00
【问题描述】:

我正在尝试进行自己的 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>

【问题讨论】:

    标签: ajax jquery input tumblr


    【解决方案1】:

    您需要将其放入complete 函数中,该函数可以作为参数传递给load

    http://api.jquery.com/load/

    Load 进行异步 AJAX 调用。您正在尝试在加载内容之前设置值。

    $('body').load("http://syndex.me/tagged/"+ syndex,function(){
        $('input:text').val(syndex);
    });
    

    【讨论】:

    • 我还建议使用比input:text 更精确的选择器,也许是id(我认为是#syndex?)。不过,这与实际问题无关。
    • 这就是为什么我
    猜你喜欢
    • 2018-07-10
    • 2011-09-05
    • 2021-11-18
    • 2013-05-19
    • 2021-07-10
    • 1970-01-01
    • 2020-03-21
    • 2014-07-08
    • 2015-05-04
    相关资源
    最近更新 更多