简体   繁体   English

jQuery加载默认页面问题

[英]jQuery load default page issue

I have this page which loads quotes. 我有此页面加载引号。 It has a search form wherein if the user types in the search keyword, it will load the results via ajax. 它具有搜索形式,如果用户输入搜索关键字,它将通过ajax加载结果。 Now, if I click on the link which should load the default page with the Quotes, instead of the search results page, nothing happens. 现在,如果我单击应该在默认页面上加载引号而不是搜索结果页面的链接,则什么也不会发生。

If I am on the home/default page itself, and click on the link, it will display a random quote, but once it displays the search page, if I click on the link to display a random quote, nothing happens. 如果我自己位于主页/默认页面上,然后单击链接,它将显示随机引用,但是一旦显示搜索页面,如果我单击链接以显示随机引用,则什么也不会发生。 Firebug shows that the function isn't being executed. Firebug显示该函数未执行。

jQuery code: jQuery代码:

            function loadQuotes() {
                $("#bquote").load("quotes_in.php");
}

     $("a#main").click(function() {
                $(this).addClass("current"); 
                $(".toggleDiv").slideUp(600);
                    loadQuotes();
                 });
                loadQuotes();  //call it once on load

            $(".bsearch").keydown(function() {
                     //create post data
                      var postData = { 
                        "search" : $(this).val()
                      };

                      //make the call
                      $.ajax({
                        type: "POST",
                        url: "quotes_in.php",
                        data: postData, //send it along with your call
                        success: function(response){
                                setTimeout(function() {
                                $('#left').html(response);
                                    $("div#smore").hide();
                                }, 250);    

                        }
                      });

                })

HTML: HTML:

             <!-- Begin Left Column -->
                <div id="left">
                     <!-- Begin Panel -->
                    <div class="check">
        <p><input type="checkbox" value="Quote" name="quote" id="quote" checked /> <label for="quote">Quote</label></p>
    <p><input type="checkbox" value="Reference" name="reference" id="reference" checked /> <label for="reference">Reference</label></p>
            </div>
         <!-- End Panel -->
          <!-- Begin Quote -->
        <blockquote id="bquote">
        </blockquote>
                                                 <!-- End Quote -->

                                        </div>
                                 <!-- End Left Column -->

  <!-- Begin Naviagtion -->
         <div id="navigation">

             <ul>
                <li><a id="main">Another?</a></li>
                <li><a id="bsearch">Search</a></li>
                <li><a id="babout">About</a></li>
                <li><a id="bcontact">Contact</a></li>
             </ul>   

         </div>
         <!-- End Naviagtion -->

PHP search: PHP搜索:

public function formatSearch($row, $search) 
            {
                $cQuote =  $this->highlightWords(htmlspecialchars($row['cQuotes']), $search);

                    return "<div class=\"swrap\">"
                . "<p id=\"s_quotes\"><img src=\"image/list1.png\" alt=\"b\" style=\"position:relative; top:2px;\">&nbsp;" . $cQuote . "&nbsp;</p>"
                . "<div id=\"smore\"><p id=\"s_arabic\">" . $this->h($row['cArabic']) . "</p>"
                . "<p id=\"s_author\"><b>~</b>&nbsp;" . $this->h($row['vAuthor']) . "</p>"
                . "<p id=\"s_reference\"><span class=\"source\">Source:</span> " . $this->h($row['vReference']) . "</p></div></div>"
                . "<img src=\"image/sep.png\" style=\"position:relative; left: 600px;\">";
            }

The new results aren't going to be acknowledged by the JavaScript as the script only acknowledges what HTML is there upon the page load. JavaScript不会确认新结果,因为脚本仅确认页面加载时存在的HTML。 For your function to be able to 'see' HTML generated via AJAX Functions, you need to use he Jquery live function http://api.jquery.com/live/ 为了使您的函数能够“查看”通过AJAX函数生成的HTML,您需要使用Jquery实时函数http://api.jquery.com/live/

Convert your current click function to something like this 将您当前的点击功能转换为如下形式

$('a#main').live('click', function() {
     $(this).addClass("current"); 
            $(".toggleDiv").slideUp(600);
                loadQuotes();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM