2017-01-27 3 views
1

現在、htmlテーブルを使用していますが、私のテーブルonclickイベントは2つのtable.twoテーブル間で競合します。さえ動作していません。onclickイベントが動作中に2つのテーブルの間で競合することがある

私のHTMLとJSコード(第一ページ)----------

<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> 

私の第2のテーブル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私はちょうど私のURLごとに時間を変更し、(このコードは使用)のみ私の体タグをリフレッシュ動作します。

+0

両方ともIDが「myTable」です。同じページにありますか? –

+0

no differnt page ........... –

+0

テーブル名を変更してもまだ動作していません... –

答えて

0

私はこれらのHTMLコードの両方のビットが同じHTMLドキュメントに表示されると思いますか?

もしそうなら、idna1()というjs関数を2回定義します。自分のテーブルの名前を、idna2()に変更したい場合があります。

+0

いいえ、別のページにあります..... –

関連する問題