2017-01-27 37 views
1

我目前正在使用html表,但我的表onclick事件是兩個table.two之間的衝突位於不同pages.but我不知道他們爲什麼衝突,有時他們甚至不工作。onclick事件工作,有時兩個表之間的衝突

我的HTML和JS代碼(第1頁)----------

<table class="w3-table-all w3-margin-top" id="myTable"> 
<tr> 
    <th style="width:22.5%;">Vendor Picture Path</th> 
    <th style="width:22.5%;">Vendor Heading</th> 
    <th style="width:22.5%;">Vendor Adding Date</th> 
    <th style="width:22.5%;">Vendor Body</th> 
    <th style="width:10%;">Add A Course</th> 
</tr> 
<?php 

mysql_connect("host", "user", "")or die("cannot connect to server"); 
    mysql_select_db("databse name")or die("cannot select DB"); 
    $sql = "select * from somewhere"; 
    $result = mysql_query($sql); 

    while($row=mysql_fetch_assoc($result)) 
    { 
     echo '<tr> 
     <td><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">'.$row["pic_path"].'</div></td>    
     <td><div onclick="getval(this)" class="cventablehead" id="ventablehead" style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll; cursor: pointer; color:red;">'.$row["heading"].'</div></td> 
     <td>'.$row["adding_date"].'</td> 
     <td><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">'.$row["body"].'</div></td> 
     <td><button onclick="idna(this)">Add</button></td> 
     </tr>'; 
    } 
?> 
    </table> 
    <script type="text/javascript"> 
    function idna(el){ 
     var rownumber = $(el).closest('tr').index(); 
     alert(document.getElementById("myTable").rows[rownumber].cells[1].textContent); 
    } 
    </script> 

我的第二個表的HTML和JS代碼(第2頁)--------- ---

<table class="w3-table-all w3-margin-top" id="myTable"> 
<tr> 
    <th style="width:14.28%;">Vendor Name</th> 
    <th style="width:14.28%;">Course Picture Path</th> 
    <th style="width:14.28%;">Course Name</th> 
    <th style="width:14.28%;">Course Code</th> 
    <th style="width:14.28%;">Course Adding Date</th> 
    <th style="width:14.28%;">course Details</th> 
    <th style="width:14.28%;">Add A Batch</th> 
</tr> 
<?php 

    mysql_connect("host", "user", "")or die("cannot connect to server"); 
    mysql_select_db("database name")or die("cannot select DB"); 
    $sql = "select * from somewhere"; 
    $result = mysql_query($sql); 

    while($row=mysql_fetch_assoc($result)) 
    { 
     echo '<tr> 
     <td id="co1"><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">'.$row["vendor_heading"].'</div></td> 
     <td id="co2"><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">'.$row["pic_path"].'</div></td>   
     <td><div onclick="coursename(this)" id="co3" style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll; cursor: pointer; color:red;">'.$row["name"].'</div></td> 
     <td id="co4"><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">'.$row["code"].'</div></td> 
     <td id="co5"><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">'.$row["adding_date"].'</div></td> 
     <td id="co6"><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">'.$row["details"].'</div></td> 
     <td><button onclick="idna1(this)">Add</button></td> 
     </tr>'; 
    } 
?> 
    </table> 
    <script type="text/javascript"> 
    function idna1(el){ 
     var rownumber = $(el).closest('td').index(); 
     alert(document.getElementById("myTable").rows[rownumber].cells[3].textContent); 
    } 
    </script> 

如果我刷新頁面(每一頁),那麼這兩個表的工作原理absolately fine.but我只是改變了自己的網址,每次和只刷新我的身體標籤(這裏用這個代碼)。

+0

他們都有id「myTable」 - 他們在同一頁上嗎? –

+0

沒有不同的頁面........... –

+0

如果我改變表名,它們仍然不能正常工作........... –

回答

0

從你的問題我猜這兩個HTML代碼位可以出現在同一個HTML文件?

如果是這樣,您將定義名爲idna1()的js函數兩次。你可能想改變它的名字,我不知道,idna2(),在你的一個表中。

+0

不,他們在不同的頁面..... –