Javascript 我没有收到任何警告信息

Javascript 我没有收到任何警告信息,javascript,jquery,Javascript,Jquery,我试图在jquery的帮助下使用alert函数获取表的行索引,但无法获得任何输出。当我点击编辑按钮时,没有任何动作 <html> <head> <script type="text/javascript"> function check(){ $("table tr").click(function() { alert( this.rowIndex ); // alert the index number of

我试图在jquery的帮助下使用alert函数获取表的行索引,但无法获得任何输出。当我点击编辑按钮时,没有任何动作

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

函数检查(){
$(“表tr”)。单击(函数(){
alert(this.rowIndex);//提醒已单击行的索引号。
});
}
请帮我做这个

代码

$("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解决方案:

首先,您需要将其放在标记的
中:

更改:

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

你的逻辑是错误的。您不需要定义
check
函数并使用
onclick
属性。您只需要
$(“table tr”)。单击(…
部分。以下是示例:

$(“表tr”)。单击(函数()
{
alert(this.rowIndex);//提醒已单击行的索引号。
});

嘿!!你忘了包括jquery。只需添加jquery链接


函数检查(){
$(“表tr”)。单击(函数(){
alert(this.rowIndex);//提醒已单击行的索引号。
});
}

首先:您缺少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>

函数检查(){
$(“表tr”)。单击(函数(){
alert(this.rowIndex);//提醒已单击行的索引号。
});
}

这是一个供您开始使用的链接,

,因为页面
函数检查(el){alert(el.rowIndex);//警告单击行的索引号。}
然后
onclick=“check(this);”
将执行$(“table tr”)这将搜索id为“table tr”的元素,不是现在的类型。有一个语法错误。这应该会有所帮助。
$(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.
    });
}});
$(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.
  });
}}); 
<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>