load()jquery显示两次Jquery的结果

时间:2016-10-16 17:39:56

标签: jquery function

我有一个功能,其中

foreach ($data as $value) {
    $finalResults[] = array("folder", $basepath . "/" . $value->filename, $value->href);
    getFolders("", parseLink($value->href), $finalResults);
}

我默认 //s > selector which define the number of Sql results given by results.php?selector=s jQuery.fn.ShowResults = function(s){ $("#results").load("results.php?selector="+s); } $("#results").ShowResults(1); $("#show1").on("click", function(e){ $("#results").ShowResults(1);}); $("#show2").on("click", function(e){ $("#results").ShowResults(2);}); $("#show3").on("click", function(e){ $("#results").ShowResults(3);}); ;

selector=1

Sql是

 <a id=show1>Show 1</a>
 <a id=show2>Show 2</a>
 <a id=show3>Show 3</a>

 <ul id=results></ul>

PHP

 $s=$_GET['selector'];
 SELECT * WHERE condition AND selector='$s'

问题在于,有时,看起来函数被调用了两次,并且显示了像这样重复的相同结果

 $results=mysql_query("SELECT * WHERE condition AND selector='$s'");

 while($res=mysql_fetch_array($results)){

 $data=$res['names'];
 echo "<li>$data</li>";
 } 

我尝试使用 <ul id=results> <li>AAA</li> <li>BBB</li> <li>CCC</li> <li>AAA</li> <li>BBB</li> <li>CCC</li> </ul> event.preventDefault();但没有解决方案

问题是如何停止此功能行为?看起来每当我点击锚点时,我会调用两次相同的函数并默认与集合冲突

1 个答案:

答案 0 :(得分:0)

mysql_fetch_array($results)返回两个关联索引的结果以及编号索引,这就是它显示两次的原因。 请改用mysql_fetch_row($results)

  

返回数组的类型取决于result_type的定义方式。通过使用MYSQL_BOTH(默认),您将获得一个包含关联索引和数字索引的数组。使用MYSQL_ASSOC,你只获得关联索引(如mysql_fetch_assoc()工作),使用MYSQL_NUM,你只获得数字索引(因为mysql_fetch_row()工作)。