获取任何点击的td jquery的值 [英] get the value of any clicked td jquery

查看:70
本文介绍了获取任何点击的td jquery的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 im托盘获取任何单击的td的值,并使用jquery或javascript在alert()窗口中显示该值.我是在互联网上搜狗"时在托盘上分配代码,但是任何人都可以做到这一点,但是无论如何我都会在此处发布...

 $("table tbody").click(function() {
  alert($(this).find('td.value').text());
});

$(document).ready(function() {
  $('table tbody').find('tr').click(function() {
    alert("row find");
    alert('You clicked row ' + ($(this).index() + 1));
  });
});

$(document).ready(function() {
  $('table tbody').click(function() {
    var i = $(this).index();
    alert('Has clickado sobre el elemento número: ' + i);
  });
});

$("table tbody").live('click', function() {
  if $(this).index() === 1) {
  alert('The third row was clicked'); // Yes the third as it's zero base index
}
}); 

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <thead>
    <tr>
      <td>id</td>
      <td>Nombre</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>miguel</td>
    </tr>
  </tbody>
</table> 

解决方案

为什么不将click事件直接附加到td?您还需要确保要包含jQuery ...

 $( "td" ).click(function() {
    alert($(this).text());
}); 

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
    <table border="1">
        <thead>
            <tr>
                <td>id</td>
                <td>Nombre</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>miguel</td>
            </tr>
        </tbody>
    </table>
</body> 

im traying to get the value of any clicked td and show this in a alert() window with jquery or javascript. I was trayin alote of code around the internet "googling" but anyone can do this but anyways i going to posted here...

$("table tbody").click(function() {
  alert($(this).find('td.value').text());
});

$(document).ready(function() {
  $('table tbody').find('tr').click(function() {
    alert("row find");
    alert('You clicked row ' + ($(this).index() + 1));
  });
});

$(document).ready(function() {
  $('table tbody').click(function() {
    var i = $(this).index();
    alert('Has clickado sobre el elemento número: ' + i);
  });
});

$("table tbody").live('click', function() {
  if $(this).index() === 1) {
  alert('The third row was clicked'); // Yes the third as it's zero base index
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <thead>
    <tr>
      <td>id</td>
      <td>Nombre</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>miguel</td>
    </tr>
  </tbody>
</table>

解决方案

Why not attach the click event directly to the td? You also need to make sure you're including jQuery...

$( "td" ).click(function() {
    alert($(this).text());
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
    <table border="1">
        <thead>
            <tr>
                <td>id</td>
                <td>Nombre</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>miguel</td>
            </tr>
        </tbody>
    </table>
</body>

这篇关于获取任何点击的td jquery的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆