繁体   English   中英

我没有收到任何警报消息

[英]I am not getting any alert message

我正在尝试使用Alert函数使用jquery来获取表的行索引,但是我无法获得任何输出。 当我单击编辑按钮时,没有任何动作

<html>
<head>
<script type="text/javascript">
    function check(){
        $("table tr").click(function() {
            alert( this.rowIndex );  // alert the index number of the clicked row.
        });
    }
</script>
</head>
 <table>
    <tr>
     <td> <input type="button" name="test" value="Edit" id="amol" onclick="check();"/> </td>
    </tr>
 </table>
</html>

请在这件事上给予我帮助!

编码

$("table tr").click(function() 
{
  alert( this.rowIndex );  // alert the index number of the clicked row.
});

设置单击表行时的处理程序。 但这又是在单击时调用的函数中设置的。 您将需要运行处理程序。 尝试:

$(document).ready(function() {
  $("table tr").click(function() 
  {
    alert( this.rowIndex );  // alert the index number of the clicked row.
  });
});

因此,它将处理程序设置在表的行上,然后无论何时单击,它都会显示警报, 而不会显示“ onclick”。 您只需要:

<table>
  <tr>
    <td> <input type="button" name="test" value="Edit" id="amol"/> </td>
  </tr>
</table>

jQuery解决方案:

首先,您需要将其放在标记的<head>中: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

更改:

<input type="button" name="test" value="Edit" id="amol" onclick="check();"/> 

至:

<input type="button" name="test" value="Edit" id="amol"/>

和jQuery snppet一样:

$(document).ready(function () {
    $("table tr").click(function () {
        alert(this.rowIndex); // alert the index number of the clicked row.
    });
});

工作小提琴

你的逻辑是错误的。 您无需定义check功能并使用onclick属性。 您只需要$("table tr").click(...部分。这是一个示例:

 $("table tr").click(function() { alert( this.rowIndex ); // alert the index number of the clicked row. }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <input type="button" name="test" value="Edit" id="amol" /> </td> </tr> </table> 

嘿!! 您忘记包括jquery。只需添加jquery链接。

<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
    function check(){
       $("table tr").click(function(){
         alert( this.rowIndex );  // alert the index number of the clicked row.
       });
    }
</script>
</head>
   <table>
    <tr>
      <td> <input type="button" name="test" value="Edit" id="amol" onclick="check();"/></td>
    </tr>
   </table>
</html>

首先:您缺少jquery库,所以请包括它:)

我发现了两种解决方案,通用和简单:

  • 通用:

下面的代码段应打印行和cel索引以单击表格单元格。

$(document).ready(function() {
    $("table > tbody > tr > td input").click(function() {
     var row_index = $(this).closest('tr').index();
     var col_index = $(this).closest('td').index();
     alert( row_index  +' ' +  col_index );  // alert the index number of the clicked row.
    });
}});
  • 简单:这将为给定的amol按钮打印行和单元格索引。

     $(document).ready(function() { $("#amol").click(function() { var row_index = $(this).closest('tr').index(); var col_index = $(this).closest('td').index(); alert( row_index +' ' + col_index ); // alert the index number of the clicked row. }); }}); 

    我不建议将其添加为单击动作,因为每次单击都会添加一个onclick事件。 最好像上面的示例一样,使用jQuery在页面加载时执行一次。

第三,请记住id在DOM页面中必须唯一,因此,如果只有一个编辑按钮-可以,但是如果每行都有一个编辑-则将其替换为css类。

只需在标题标签中添加jquery的引用

<head>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
    function check(){
       $("table tr").click(function(){
         alert( this.rowIndex );  // alert the index number of the clicked row.
       });
    }
</script>
</head>

这是您开始使用jQuery库的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM