获取任何单击的jquery的值
get the value of any clicked td jquery
我正在尝试获取任何点击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>
您需要包含jQuery
库才能开始使用它。然后将点击事件绑定到td
,您应该会看到alert
弹出。
<head>
<title></title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js" type="text/javascript">
// Your code comes here
同样,下次如果有些东西不工作,你应该做的第一件事就是打开控制台,检查你是否看到任何错误并采取行动。
- 使用
console.log
代替alert
应该是alert
完全阻塞UI线程的方式。
$("td").click(function() {
alert($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.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>
相关文章:
- 如何使用jquery在填充自动完成的值后使文本框只读
- 如何使用jQuery选择下拉列表的值
- 如何设置输入文本框jquery的值
- 复选框组获取选中复选框jquery的值
- 捕获隐藏输入元素jQuery的值
- 更改动态生成的输入字段 jQuery 的值
- 根据选项值的文本而不是使用 jQuery 的值设置下拉列表的选定值
- 使用 href Jquery 的值更新输入
- 正在获取<text区域>jQuery的值未按预期工作
- 忽略选择选项jQuery的值
- 提交表单时如何回显滑块范围jquery的值以及如何设置滑块范围$_GET值的值
- MVC 5 DropDownList向Jquery的值添加字符串
- 从外部php文件返回一个用于jQuery的值
- 我想显示消息一旦每个字段有使用jquery的值
- 将数据属性设置为按钮并链接所述属性jquery的值
- 我如何访问PHP变量,以便我可以使用javascript/ jquery的值工作
- 修改第一个输入jquery的值
- JQuery的值不会改变
- 获取任何单击的jquery的值
- 如何在aspx页面中添加jQuery的值,在服务器端点击提交按钮