【发布时间】:2016-02-16 13:30:10
【问题描述】:
有人可以帮我破解这个吗?单击以显示对话框时,我有一个带有事件的 HTML 表。我也想得到点击行的行索引,但它只输出不同的东西。
//script for the table event
<script>
$(function() {
$( "#table").click(function(){
$( "#dialog-modal1" ).dialog({
height: 140,
modal: true,
title:"Search Item",
width : 440,
position: {my: "top", at: "bottom", of: "#table"}
});
$( "#dialog-modal1" ).show();
var row = $('<tr></tr>').text($(this).index());
alert(row);
});
});
</script>
单击表时对话框按预期显示,但行索引的输出不是我想要的。它总是输出 [object Object]。如果点击表格的第一行,我希望输出是 0,第二行应该是 1 等等。我做错了什么?
【问题讨论】:
-
你应该使用
console.log(row)而不是alert(row),并在控制台中观察结果。 -
@cl3m console.log(row) 输出 [tr] 。仍然不是我想要的。
-
您的代码中有很多错误。 1. 在您的“点击”处理程序中,“this”实际上是
table元素,而不是tr。 2.rowvar 是tr,而tr不能包含文本,只能包含td或th... 3. 你会看到[object Object],因为rowvar很好……一个对象。您可能想提醒.index()值... 4. 查看我的答案:-)
标签: javascript html jquery-ui jquery-events