我没有得到任何警报信息

时间:2014-12-03 07:24:03

标签: javascript jquery

我试图使用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>

请帮助我!

6 个答案:

答案 0 :(得分:0)

代码

$("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.
  });
});

因此,它会在表格的行上设置处理程序,然后无论何时单击,它都会显示警告,不带&#34; onclick&#34;。你只需要:

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

答案 1 :(得分:0)

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.
    });
});

Working Fiddle

答案 2 :(得分:0)

你的逻辑错了。您无需定义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>

答案 3 :(得分:0)

  

喂!!你忘了包含jquery.just添加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>

答案 4 :(得分:0)

首先:你缺少jquery库,所以请包含它:)

我找到了两个通用和简单的解决方案:

  • 通用:
在片段下面的

应该打印row和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在页面加载时执行一次会更好,就像上面的示例一样。

第三,请记住,在DOM页面中id必须是唯一的,所以如果只有一个编辑按钮 - 它没问题,但是如果每行有一个编辑 - 用css类替换它。

答案 5 :(得分:0)

只需在标题标记

中添加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 library

的链接
相关问题