如何在php表中执行javascript函数


How to execut a javascript function in a php table

我正在开发一个小网页。

在这个页面上,我有一个表,并创建了一个获取id并返回图像的函数。

一切都正常工作,问题是每当我点击一行时,就会执行该函数。

我只是想在点击id的附加列时执行该函数。(第二列)

有人能帮我吗?

意识到我的疑虑了吗?

谢谢大家。


    <script>
function addRowHandlers() {
    var table = document.getElementById("tableId");
    var rows = table.getElementsByTagName("td");
    for (i = 0; i < rows.length; i++) {
        var currentRow = table.rows[i];
        var createClickHandler = 
            function(row) 
            {
                return function() { 
                                        var cell = row.getElementsByTagName("td")[1];
                                        var id = cell.innerHTML;
                                        //use ajax to post id to ajax.php file
                                        $.post("ajax.php", { id:id }, function( data ) { 
                                              //add result to a div and create a modal/dialog popup similar to alert()
                                              $('<div />').html(data).dialog();
                                        });
                                 };
            };
        currentRow.onclick = createClickHandler(currentRow);
    }
}
window.onload = addRowHandlers();
</script>

您已经在使用jQuery了,所以…让我们使用jQuery:

// Process the code on document ready
$(function() {
    // Hook "click" on the table, but only actually call the handler if the
    // click passed through the second cell in a row
    $("#tableId").on("click", "tr td:nth-child(2)", function() {
        // Do the post
        $.post("ajax.php", {
            id: $(this).text() // <== Getting the id by getting the text of the clicked cell
        }, function(data) {
            //add result to a div and create a modal/dialog popup similar to alert()
            $('<div />').html(data).dialog();
        });
    });
});

差异:

  1. 我们是在文档就绪而不是窗口加载的情况下进行的,这要早得多。

  2. 在每个表单元格上使用委托的处理程序而不是处理程序。

  3. 使用:nth-child(2)来获得该行中的第二个td。请注意,这假设您的行中只有单元格(这是99.9999%的正确值;不过template也是允许的)。

  4. $(this).text()获得id

下面是一个只显示id而不是执行$.post调用的示例:

// Process the code on document ready
$(function() {
    // Hook "click" on the table, but only actually call the handler if the
    // click passed through the second cell in a row
    $("#tableId").on("click", "tr td:nth-child(2)", function() {
        alert($(this).text());
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="tableId">
  <tbody>
    <tr>
      <td>Foo</td>
      <td>23</td>
      <td>Bar</td>
    </tr>
    <tr>
      <td>Foo</td>
      <td>42</td>
      <td>Bar</td>
    </tr>
    <tr>
      <td>Foo</td>
      <td>67</td>
      <td>Bar</td>
    </tr>
  </tbody>
</table>

为什么你没有尝试id意味着给每个td的id,无论你想在onclick函数上做什么,你都可以做

将点击处理程序放在单元格上,而不是放在行上

var currentRow = table.rows[i];
var cell = currentRow.getElementsByTagName("td")[1];
var createClickHandler = function(cell) 
    {
        return function() { 
            var id = cell.innerHTML;
            //use ajax to post id to ajax.php file
            $.post("ajax.php", { id:id }, function( data ) { 
                  //add result to a div and create a modal/dialog popup similar to alert()
                  $('<div />').html(data).dialog();
            });
         };
    };
cell.onclick = createClickHandler(cell);