load()函数添加的内容在ajax操作后消失

时间:2013-03-01 15:36:06

标签: php html mysql ajax load

我正在创建一个排序页面,它将显示数据库的内容,用户可以使用listjs对其进行排序。问题是当我按任何按钮时,从数据库加载的内容消失。当我手动填充内容时,它工作得很好。有人可以帮我解决这个问题吗?

我的加载功能:

$(document).ready(function(){
     $(".list").load("../wp-content/php/sorting.php");
});

sorting.php

<?php
mysql_connect("***", "***", "**") or die(mysql_error());
mysql_select_db("***");

$result = mysql_query("SELECT * FROM business");

while($row = mysql_fetch_array($result))
  {
 ?><li class="onebusiness"><a href="<?php
  echo $row['link'];?>"><img src="<?php
  echo $row['img'];?>" height="125" width="125"/><p class="name"><?php
  echo $row['name'];?></p><p class="category" style="display:none;"><?php
  echo $row['category'];?></p></a></li><?php
  }
mysql_close();
?> 

HTML:

<script src="../wp-content/js/filter/call-php.js" type="text/javascript"></script>
<script src="../wp-content/js/list/list-min.js" type="text/javascript"></script>
<div id="busineseslist">
    <input class="search" placeholder="Search Business" />
    <ul class="sort-by">
        <li class="sort btn" data-sort="name">Sort by name</li>
        <li class="sort btn" data-sort="category">Sort by category</li>
    </ul>
    <ul class="filter">
        <li class="btn" id="filter-none">Show all</li>
        <li class="btn" id="filter-something">Only show something</li>
        <li class="btn" id="filter-else">Only show something else</li>
    </ul>
    <div class="list"></div>
</div>

1 个答案:

答案 0 :(得分:0)

为什么你不这样做:

<script src="../wp-content/js/filter/call-php.js" type="text/javascript"></script>
<script src="../wp-content/js/list/list-min.js" type="text/javascript"></script>
<div id="busineseslist">
<input class="search" placeholder="Search Business" />
<ul class="sort-by">
    <li class="sort btn" data-sort="name">Sort by name</li>
    <li class="sort btn" data-sort="category">Sort by category</li>
</ul>
<ul class="filter">
    <li class="btn" id="filter-none">Show all</li>
    <li class="btn" id="filter-something">Only show something</li>
    <li class="btn" id="filter-else">Only show something else</li>
</ul>
<div class="list"><?php include '../wp-content/php/sorting.php'; ?></div>
</div>

有了这个,你可以删除这一行:

<script src="../wp-content/js/filter/call-php.js" type="text/javascript"></script>//delete this

我认为它的js包含以下代码:

$(document).ready(function(){
 $(".list").load("../wp-content/php/sorting.php");
});

因为php包含你不需要加载功能;)

------------------------ EDITED ---------------------- ------------------

在wordpress中,您可以通过以下方式包含文件:

将包含文件放在主题文件夹中,并在主题的footer.php中使用:

 <?php include (TEMPLATEPATH . '/sorting.php'); ?> 

请参阅此参考链接:http://www.deluxeblogtips.com/2010/06/wordpress-include-template-files.html

你的div应该是这样的:

转到“主题编辑器”,查找生成html的文件。编辑它并将此代码放在divs标记之间:

<div class="list"><?php include (TEMPLATEPATH . '/sorting.php'); ?></div> 

Saludos。