jQuery的行选择按钮onClick [英] Jquery row selecting in a button onClick

查看:76
本文介绍了jQuery的行选择按钮onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < tr>我有一个表格(表格的ID为县级表格) 
< td>< input class =texttype = text name = param1 maxlength = 2 size = 2>< / td>
< td>< select class =textname = param2>< option value = 1> Live< option value = 2> Test< / select>< / td>
< td>< input class =texttype = text name = param3>< / td>
< td>< input class =texttype = text name = param4>< / td>
< td>< input class =texttype = text name = param5>< / td>
< td>< input class =texttype = text name = param6>< / td>
< td colspan = 2>< input class =buttontype = submit value = New onclick =function_new($('#county_table tr:eq(1)'))>< TD>
< / tr>

onclick函数可以管理表格数据,也可以发送AJAX,所以我需要该表的所有元素。



该解决方案的坏处是,如果我插入一个新行,整个表格行选择就会混乱起来,所以我想改变 tr:eq(1),就像 this.tr 或类似的东西,他的祖先,并得到给定的对象?



我将不胜感激任何帮助。

解决方案

我建议你使用 $(this).parent()。parent()来获得更好的性能。
http://jsperf.com/parentparentvsclosest/2



但是,如果您正在寻找代码简单性,那么您可以为 $(this).closest('tr')


I have a table (id of the table is county_table) with plenty of lines like this:

<tr>
<td><input class="text" type=text name=param1 maxlength=2 size=2></td>
<td><select class="text" name=param2><option value=1>Live<option value=2>Test</select></td>
<td><input class="text" type=text name=param3 ></td>
<td><input class="text" type=text name=param4 ></td>
<td><input class="text" type=text name=param5 ></td>
<td><input class="text" type=text name=param6 ></td>
<td colspan=2><input class="button" type=submit value=New onclick="function_new($('#county_table  tr:eq(1)'))"></td>
</tr>

The onclick function would manage the table data, and maybe send an AJAX, so I need to get the all elements of the table.

The bad thing with that solution is that if I insert a new row the whole table row selection would be messed up, so I'd like to change the tr:eq(1), something like this.tr or something like that, is the button know his ancestor and get the given object?

I would appreciate any help.

解决方案

I would recommend you to use $(this).parent().parent() for a better performance. http://jsperf.com/parentparentvsclosest/2

But if you are looking for code simplicity, then you could go for $(this).closest('tr').

这篇关于jQuery的行选择按钮onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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