2013-10-04 46 views
0

我想要一個表格,其中包含數據庫中的單詞及其含義,在另一列中,我希望每行都有複選框,該用戶將檢查它們並顯示他/她知道的單詞。 我在這種情況下有兩個問題: 1-我怎樣才能隱藏在第一個意思,並在點擊後顯示意義可見他們? 2-如何設置複選框? 我有這樣的代碼,到現在爲止,但它不工作 請幫助我,如果你能php中的表格中的複選框

<script type="text/javascript"> 
     function ShowMeanings(){ 
     document.getElementsByClassName('hiding').item(0).style.visiblility = 'visible'; 
     } 
</script> 
<?php 
    $con = mysql_connect("localhost", "root", "") 
    or die(mysql_error()); 
    if (!$con) { 
             die('Could not connect to MySQL: ' . mysql_error()); 
            } 
            mysql_select_db("project", $con) 
            or die(mysql_error()); 
            $result = mysql_query("select * from words"); 
            echo "<table border='1'> 
             <tr> 
             <th>word</th> 
             <th>meaning</th> 
             <th>check</th> 
             </tr>"; 
            while($row = mysql_fetch_array($result)) 
             { 
             echo "<tr>"; 
             echo "<td>" . $row['word'] . "</td>"; 
             echo "<td>"; 
             echo "<div"; 
             echo "class='hiding' style='visibility:hidden'>" . $row['meaninig']; 
             echo "</div>"; 
             echo "</td>"; 
             echo "<td>"; 
             echo "<input"; 
             echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />"; 
             echo "</td>"; 
             echo "</tr>"; 
             } 
             echo "</table>"; 
             mysql_close($con);                     
           ?> 

           </div> 
           <button onclick="ShowMeanings()">showmeaning</button> 
+0

你通過任何改變知道jquery嗎? – magicianiam

+0

可以顯示任何錯誤或調試代碼。另外$ row ['meaninig']拼寫錯誤...不知道這是否是由設計? :) – devfunkd

+0

我知道這是missplelled,但我沒有改變我的表在數據庫中的列 我hava erro在這裏: document.getElementsByClassName(...)。item(...)是空的document.getElementsByClassName ('hiding')。item(0).style.visiblility ='visible' – fahimeh

回答

0

爲了隱藏一個建議:

echo "class='hiding' style='display:none'>" . $row['meaninig']; 

要顯示的含義:

//for Jquery 

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
<script type="text/javascript"> 
     function ShowMeanings(){ 
      $('.hiding').shoW(); 
     } 
</script> 

//for plain old javascript 
    <script type="text/javascript"> 
       function ShowMeanings(){ 
        document.getElementsByClassName('hiding').style.visibility = 'visible'; 
       } 
     </script> 

您的代碼編輯:

<html><head> 
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
<script type="text/javascript"> 
     function ShowMeanings(){ 
      $('.hiding').shoW(); 
     } 
</script> 
</head> 
<body> 
<?php 
    $con = mysql_connect("localhost", "root", "") 
    or die(mysql_error()); 
    if (!$con) { 
             die('Could not connect to MySQL: ' . mysql_error()); 
            } 
            mysql_select_db("project", $con) 
            or die(mysql_error()); 
            $result = mysql_query("select * from words"); 
            echo "<table border='1'> 
             <tr> 
             <th>word</th> 
             <th>meaning</th> 
             <th>check</th> 
             </tr>"; 
            while($row = mysql_fetch_array($result)) 
             { 
             echo "<tr>"; 
             echo "<td>" . $row['word'] . "</td>"; 
             echo "<td>"; 
             echo "<div"; 
             echo "class='hiding' style='display:none'>" . $row['meaninig']; 
             echo "</div>"; 
             echo "</td>"; 
             echo "<td>"; 
             echo "<input"; 
             echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />"; 
             echo "</td>"; 
             echo "</tr>"; 
             } 
             echo "</table>"; 
             mysql_close($con);                     
           ?> 

           </div> 
           <button onclick="ShowMeanings()">showmeaning</button> 
    </body> 
+0

我的代碼如下: echo「class ='hiding'style ='visibility:hidden'>」。 $行[ 'meaninig']; 但功能showmeaning不起作用 我想有第三列,其中包含複選框,用戶可以檢查它(檢查他/她知道的含義與否) – fahimeh

+0

這是一個建議,也是什麼意思你的號碼2 – magicianiam

+0

@fahimeh所以你想要的是顯示所有複選框的所有隱藏含義的含義?更新我的代碼,顯示按鈕點擊時的所有含義,如果您希望它只查看每個複選框的特定含義,我會建議您更改您的代碼 – magicianiam

0

getElementByClassName是inexistant功能。你的意思是getElementsByClassName,然而它會返回一個元素列表,所以你需要選擇一個元素。

document.getElementsByClassName('hiding').item(0).style.visibility = 'visible';

+0

你會嘗試這個 – Colandus

+0

@fahimeh我想你說的 但確實,T工作:( – fahimeh

+0

你確定嗎?我甚至測試它和正常工作。您的實現必須是錯誤的。儘量使用'alert'或類似在你的'ShowMeanings'功能,看是否'onclick'事件真的跑 – Colandus