jquery获取table中点击位置所在行的id

jquery获取table中点击位置所在行的id,第1张

$("td")click(function(){

  //td的id 

  alert($(this)attr("id"));

  //tr的id  

  alert($(this)parent()attr("id"));

});

js:

$(function(){

var myRows = $('table tr')click(function(){

alert('Row #' +myRowsindex(this) + ' value=' + $(this)children('td')text());

});

});

html:

<table width="300" border="1" cellspacing="1" cellpadding="2">

  <tr>

    <td width="150">0-1</td>

    <td width="150">0-2</td>

  </tr>

  <tr>

    <td>1-1</td>

    <td>1-2</td>

  </tr>

  <tr>

    <td>2-1</td>

    <td>2-2</td>

  </tr>

  <tr>

    <td>3-1</td>

    <td>3-2</td>

  </tr>

  <tr>

    <td>4-1</td>

    <td>4-2</td>

  </tr>

</table>

你试下看,是否是你要的

当都选其中某几项时,如果需要给服务端ajax传递对应项的id值,该如何做呢?

思路应该是这样的:

①获取被勾选项(即checked的checkbox所在行

②将id存在checkbox所在input的自定义属性中,如 data-id 中

③创建空数组,遍历被选中的checkbox所在行,获取对应的 data-id 值,存入数组中

④将存入的数组转化为字符串,并用逗号隔开(join(','))

表格也是一个jqgrid,你可以像 *** 作主表格一样 *** 作子表格的

subGridRowExpanded: function(subgrid_id, row_id) {

// we pass two parameters

// subgrid_id is a id of the div tag created whitin a table data

// the id of this elemenet is a combination of the "sg_" + id of the row

// the row_id is the id of the row

// If we wan to pass additinal parameters to the url we can use

// a method getRowData(row_id) - which returns associative array in type name-value

// here we can easy construct the flowing

var subgrid_table_id, pager_id;

subgrid_table_id = subgrid_id+"_t";

pager_id = "p_"+subgrid_table_id;

$("#"+subgrid_id)html("<table id='"+subgrid_table_id+"' class='scroll'</table<div id='"+pager_id+"' class='scroll'</div");

jQuery("#"+subgrid_table_id)jqGrid({

url:"subgridphpq=2&id="+row_id,

datatype: "xml",

colNames: ['No','Item','Qty','Unit','Line Total'],

colModel: [

{name:"num",index:"num",width:80,key:true},

{name:"item",index:"item",width:130},

{name:"qty",index:"qty",width:70,align:"right"},

{name:"unit",index:"unit",width:70,align:"right"},

{name:"total",index:"total",width:70,align:"right",sortable:false}],

rowNum:20,

pager: pager_id,

sortname: 'num',

sortorder: "asc",

然后子表格的 *** 作就是jQuery("#"+subgrid_table_id)jqGrid('getGridParam','selarrrow');

$(document)ready(function(){

var button=$("button");//找到被点击的按钮

//定义按钮点击事件

buttonclick(function(){

var clickRow=$(this)parents("tr")[0];//获取被点击按钮所在的行, 返回的是javascript 的DOM对象,要注意

});

});

以上就是关于jquery获取table中点击位置所在行的id全部的内容,包括:jquery获取table中点击位置所在行的id、jquery判断表格中是否有行被选中、jQuery获取表格中checkbox被选中一行的属性数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9272792.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存