2012-02-11 30 views
2

我不知道如何让它工作。我解释一下。jquery从数据中选择索引并显示sql内容

我有一些与jQuery脚本的HTML代码。

HTML

<div id="accordion"> 
    <h3><a class="0" href="#">aparamenta modular</a></h3> 
    <div> 
    <div class="cat_content_ex"> 
     t&eacute;rmicos, diferenciales, etc ... 
    </div> 
    </div> 
    <h3><a class="1" href="#">iluminaci&oacute;n</a></h3> 
    <div> 
    <div class="cat_content_ex"> 
     luminarias std y sistemas de LED, etc ... 
    </div> 
    </div> 
</div> 
<div id="target"></div> 

脚本

<script type="text/javascript"> 
    $('h3 > a').live('click', function(){ 
     var currentClass = $(this).attr('class'); 
     alert (currentClass, 'alert window'); 
     if(currentClass == '0') { 
      <?php 
       include "./connection_catalogs.php";     
       $SQL = "SELECT * FROM first_table"; 
       $result = mysql_query($SQL); 
       while ($row = mysql_fetch_array($result)) { ?> 
        $('#target').html($(<?php echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";?>)); 
       } 
     } else if(ctg == '1') { 
      <?php 
       include "./connection_catalogs.php";     
       $SQL = "SELECT * FROM second_table"; 
       $result = mysql_query($SQL); 
       while ($row = mysql_fetch_array($result)) { ?> 
        $('#target').html($(<?php echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";?>)); 
       } 
       } 
      ?> 
     } 
}); 
</script> 

用户点击在手风琴的第一头和显示SQL表中的内容通过执行第二头echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";

用户点击一个名为 'first_table'的手风琴,并通过执行echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";

来显示名为“second_table”的sql表的内容
+3

可否请你试图澄清,什么是问题N + – 2012-02-11 11:40:33

+0

是的,我喜欢。问题是,这是行不通的。我希望用户点击每个手风琴的标题,并根据哪个标题点击它将显示每个内容从SQL表或其他。 – 2012-02-11 11:49:38

+0

用户点击“h3”后,您希望如何显示内容? – 2012-02-11 11:57:20

回答

2

$('#accordion.h3')选择一个带有id="accordion" class="h3"的元素。

将其更改为$('#accordion > h3'),其中选择一个<h3>元素,该元素具有id="accordion"的父元素。

+0

是的,它确定但脚本不会返回既不alert也不SQL数据...... :( – 2012-02-11 20:25:00

1
while ($row = mysql_fetch_array($result)) { ?> 
    document.write(<?php echo "<p class='ctg_fab'>" .$row['fabricante']. " : " .$row['titulo']. " : <a href='" .$row['url']. "'>descarga</a> : tipo - " .$row['filetype']. "</p>";?>) 
} 
+0

不知道为什么我应该使用document.write函数...? – 2012-02-11 19:49:28

+0

它工作与否?因为你把php和javascript混合起来,结果是,两个概念都混淆了(混淆你)。 PHP的回声只是将结果打印到JavaScript代码中,而不是在文档/ html中。 document.write是javascipt的函数,用于在文档中显示它。无论如何,尽量让你的代码更简单,更容易阅读,所以混乱不会再发生:) – bondythegreat 2012-02-12 01:32:49

0

终于我在阿贾克斯制造。

调用JScript函数时,在链接用户点击

HTML

<div id="accordion"> 
    <h3><a onClick= MuestraCatalogo(1) href="#">aparamenta modular</a></h3> 
    <div> 
    <div class="cat_content_ex"> 
     t&eacute;rmicos, diferenciales, etc ... 
    </div> 
    </div> 
    <h3><a onClick= MuestraCatalogo(2) href="#">iluminaci&oacute;n</a></h3> 
    <div> 
    <div class="cat_content_ex"> 
     luminarias std y sistemas de LED, etc ... 
    </div> 
    </div> 
</div> 
<div id="target"></div> 

脚本

function MuestraCatalogo(str) 
{ 
if (str=="") 
    { 
    document.getElementById("cat_rcpt").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("cat_rcpt").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","auth/peticion_catalogo.php?q="+str,true); 
xmlhttp.send(); 
} 

peticion_catalogo.php

<?php 
    $q=$_GET["q"]; 
    include "../connection_catalogs.php"; 
    if($q == 1){ 
      $SQL = "SELECT * FROM aparamenta_modular"; 
    } else if($q == 2){ 
      $SQL = "SELECT * FROM control_industrial"; 
    } 
    $result = mysql_query($SQL); 
    echo "<table class='ctgPage'>"; 
    echo "<tr>"; 
    echo "<td class='ctgCabPage' style='width:150px'>FABRICANTE</td>"; 
    echo "<td class='ctgCabPage' style='width:300px'>CAT&Aacute;LOGO</td>"; 
    echo "<td colspan='2' class='ctgCabPage' style='width:150px'>ENLACE</td>"; 
    echo "</tr>"; 
    while ($row = mysql_fetch_array($result)) { 
     echo "<td class='ctgCabData' style='width:150px'>" .$row['fabricante']. "</td>"; 
     echo "<td class='ctgCabData' style='width:300px'>" .$row['titulo']. "</td>"; 
     if($row['filetype']=="pdf") { 
      $url_icon = "http://www.domain.com/img/pdf.png"; 
     } else if($row['filetype']=="xls") { 
      $url_icon = "http://www.domain.com/img/xls.png"; 
     } else if($row['filetype']=="rar") { 
      $url_icon = "http://www.domain.com/img/rar.png"; 
     } 
     echo "<td colspan='2' class='ctgCabData' style='width:150px'><a href='" .$row['url']. "'><img src='http://www.domain.com/img/download.png' alt='descarga'></a>&nbsp;&nbsp;<img src='" .$url. "' alt='type'></td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
    mysql_close($con); 
?>