【问题标题】:I am not getting any alert message我没有收到任何警报消息
【发布时间】:2016-02-02 03:43:14
【问题描述】:

我试图在 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>

请帮帮我!

【问题讨论】:

  • 因为页面function check(el) { alert(el.rowIndex); // alert the index number of the clicked row. } 中没有包含jQuery,所以onclick="check(this);" 就可以了
  • $("table tr") 这会搜索 id 为 "table tr" 的元素,而不是原来的类型。
  • 存在语法错误。这应该会有所帮助。 stackoverflow.com/questions/4524661/…>

标签: javascript jquery


【解决方案1】:

jQuery 解决方案:

首先你需要把它放在标记的&lt;head&gt; 中:<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】:

    代码

    $("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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        • 通用:

        sn-p 下面应该打印行和 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类替换它。

        【讨论】:

          【解决方案5】:

          嘿!!您忘记包含 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>
          

          【讨论】:

            【解决方案6】:

            只需在header标签中添加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 的链接

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-06-14
              • 2018-11-21
              • 1970-01-01
              • 2022-09-24
              • 1970-01-01
              • 1970-01-01
              • 2021-11-19
              • 2021-05-14
              相关资源
              最近更新 更多