表和foreach循环中的onclick事件

时间:2014-09-18 09:13:27

标签: javascript php codeigniter onclick

我想允许在codeigniter中单击foreach循环中的整个主机表。出于某种原因,代码对我不起作用。你知道如何解决它吗?

提前致谢

<table class="table table-hover">
    <thead>
        <tr>
            <th>השולח</th>
            <th>נשלחה בתאריך</th>
            <th>נושא ההודעה</th>
            <th>תוכן ההודעה</th>

        </tr>
    </thead>
    <tbody>


       <?php foreach ($messages->result() as $row)
             { 
       ?> 
                <script type="text/javascript">
                   function newDoc() {
                       window.location.assign("<?php echo base_url();?>/read_messages/<?php echo $row->id;?>")
                   }
                </script>

       <?php

                echo "<tr>";
                echo "<td>".$row->author. "</td>";
                echo "<td>".$row->posted_in. "</td>";
                echo "<td>".$row->title. "</td>";
                echo "<td>".$row->body. "</td>";

       ?>

                onclick="newDoc()";

        <?php   
                echo "</tr>";   
              }
        ?>    

    </tbody>
</table>
</div>

3 个答案:

答案 0 :(得分:0)

你必须分配onClick =&#34; newDoc&#34; html元素如

&#13;
&#13;
<table onClick="newDoc()">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您必须解决问题,一个newDoc()函数未分配给元素,而您想要两个函数创建newDoc函数一次且仅传递$row->id一次,以便其影响发生变化对于每个<tr>

<script type="text/javascript">
  function newDoc(rowId) {
    window.location.assign("<?php echo base_url();?>/read_messages/" + rowId);
  }
</script>

<?php
 foreach ($messages->result() as $row) {
   echo "<tr  onlick='newDoc(<?php echo $row->id ?>)'>";
   echo "<td>".$row->author. "</td>";
   echo "<td>".$row->posted_in. "</td>";
   echo "<td>".$row->title. "</td>";
   echo "<td>".$row->body. "</td>";
   echo "</tr>"; 
 }
?>

答案 2 :(得分:0)

据我了解你的问题,你想要点击每一行。那么你的代码应该是:

你的剧本应该在一边

<script type="text/javascript">
  function newDoc(id) {
     window.location.assign("<?php echo base_url();?>/read_messages/"+id);
  }
</script>


<?php foreach ($messages->result() as $row)
      { 
                echo "<tr onclick='newDoc(".$row->id.")'>";
                echo "<td>".$row->author. "</td>";
                echo "<td>".$row->posted_in. "</td>";
                echo "<td>".$row->title. "</td>";
                echo "<td>".$row->body. "</td>";

                echo "</tr>";   
      }
?>