使用 jQuery 对表行进行重新排序,但排除嵌套表
我试图从我的重新排序表脚本中排除嵌套表/tr。我已经研究了 JQuery 和嵌套表的主题。我尝试的一切都只成功了一半。 $("#table tr.table_row1") 或 $("table > tr.order_rows")
它位于 for 循环中并从数组中获取顺序。我将不胜感激任何帮助。
这是我的代码:
<table>
<tr class="order_rows"> <- pick this row
<td>
<table>
<tr> <- dont pick this
<td><input type="text" name="order1" value="1" id="order1" size="2" /></td>
<td></td>
</tr>
<tr> <- dont pick this row
<td>...</td>
</tr>
</table>
</td>
</tr>
<tr class="order_rows"> <- pick this row
<td>
<table>
<tr> <- dont pick this row
<td><input type="text" name="order1" value="1" id="order1" size="2" /></td>
</tr>
<tr> <- dont pick this row
<td>...</td>
</tr>
</table>
</td>
</tr>
</table>
这是我正在处理的 JQuery。
function reorder(NewOrder)
{
var orderedTrs = new Array();
alert(NewOrder);
for ( i=0; i<=NewOrder.length; i++ ) orderedTrs[i] = $("#table tr")[NewOrder[i]];
alert(orderedTrs);
for ( i=0; i<=NewOrder.length; i++ ) $("#table").append(orderedTrs[i]);
}
$(document).ready(function()
{
$(".updateButton:button").click(function()
{
var NewOrder = new Array;
NewOrder[0] = parseInt($("#order1").val());
NewOrder[1] = parseInt($("#order2").val());
reorder(NewOrder);
});
});
I'm trying to exclude nested tables/tr from my Reorder Table script. I have looked at the topic of JQuery and nest tables. Everything I try only work half way.
$("#table tr.table_row1") or $("table > tr.order_rows")
it's in a for loop and picks up the order from an array. I would appreciate any help.
Heres' my code:
<table>
<tr class="order_rows"> <- pick this row
<td>
<table>
<tr> <- dont pick this
<td><input type="text" name="order1" value="1" id="order1" size="2" /></td>
<td></td>
</tr>
<tr> <- dont pick this row
<td>...</td>
</tr>
</table>
</td>
</tr>
<tr class="order_rows"> <- pick this row
<td>
<table>
<tr> <- dont pick this row
<td><input type="text" name="order1" value="1" id="order1" size="2" /></td>
</tr>
<tr> <- dont pick this row
<td>...</td>
</tr>
</table>
</td>
</tr>
</table>
This is my JQuery that I'm working on.
function reorder(NewOrder)
{
var orderedTrs = new Array();
alert(NewOrder);
for ( i=0; i<=NewOrder.length; i++ ) orderedTrs[i] = $("#table tr")[NewOrder[i]];
alert(orderedTrs);
for ( i=0; i<=NewOrder.length; i++ ) $("#table").append(orderedTrs[i]);
}
$(document).ready(function()
{
$(".updateButton:button").click(function()
{
var NewOrder = new Array;
NewOrder[0] = parseInt($("#order1").val());
NewOrder[1] = parseInt($("#order2").val());
reorder(NewOrder);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有评估其余代码的逻辑,但对您的问题最明显的答案是将您的选择器从 修改为 ,
从而
确保您仅选择直接子 TR 而不是所有后代 TR。
I haven't evaluated the logic of the rest of your code, but the most obvious answer to your issue would be to modify your selector from
to
thus ensuring that you select only direct child TRs and not all descendant ones.