如何获取行索引并将其作为参数发送到javascript函数
我使用的是一个包含一些行的简单html文件,我需要获取单击行的rowindex,并将其作为参数发送给我的一个自定义javascript方法。 我不会使用任何gridview或jquery 我的html应该是这样的如何获取行索引并将其作为参数发送到javascript函数,javascript,html,Javascript,Html,我使用的是一个包含一些行的简单html文件,我需要获取单击行的rowindex,并将其作为参数发送给我的一个自定义javascript方法。 我不会使用任何gridview或jquery 我的html应该是这样的 <html> <head> <body> <form name="myForm"> <table id="mainTable" border="1" width="100%"> <script>
<html>
<head>
<body>
<form name="myForm">
<table id="mainTable" border="1" width="100%">
<script>
document.write('<tr>')
document.write('<td>')
document.write('row1')
document.write('</td>')
document.write('</tr>')
document.write('<tr>')
document.write('<td>')
document.write('row2')
document.write('</td>')
document.write('</tr>')
document.write('</table>')
document.write('<table>')
document.write('<tr>')
document.write('<td>')
document.write('<input type="button" value=" move UP " onClick="swapRowUp(getRowIndex(this))"</input>')>
document.write('<input type="button" value="move DOWN" onClick="swapRowDown(getRowIndex(this))"</input>')>
document.write('</td>')
document.write('</tr>')
document.write('</table>')
</script>
</table>
</form>
</body>
</head>
</html>
<script>
var mainTable = document.getElementById("mainTable");
function getRowIndex(el)
{
while( (el = el.parentNode) && el.nodeName.toLowerCase() !== 'tr' );
if( el )
alert(el.rowIndex);
return el.rowIndex;
}
function swapRowUp(chosenRow) {
if (chosenRow.rowIndex != 0) {
moveRow(chosenRow, chosenRow.rowIndex-1);
}
}
function swapRowDown(chosenRow) {
if (chosenRow.rowIndex != mainTable.rows.length-1) {
moveRow(chosenRow, chosenRow.rowIndex+1);
}
}
function moveRow(targetRow, newIndex) {
if (newIndex > targetRow.rowIndex) {
newIndex++;
}
var mainTable = document.getElementById('mainTable');
var theCopiedRow = mainTable.insertRow(newIndex);
for (var i=0; i<targetRow.cells.length; i++) {
var oldCell = targetRow.cells[i];
var newCell = document.createElement("TD");
newCell.innerHTML = oldCell.innerHTML;
theCopiedRow.appendChild(newCell);
copyChildNodeValues(targetRow.cells[i], newCell);
}
//delete the old row
mainTable.deleteRow(targetRow.rowIndex);
}
function copyChildNodeValues(sourceNode, targetNode) {
for (var i=0; i < sourceNode.childNodes.length; i++) {
try{
targetNode.childNodes[i].value = sourceNode.childNodes[i].value;
}
catch(e){
}
}
}
</script>
文件。写入(“”)
文件。写入(“”)
document.write('row1')
文件。写入(“”)
文件。写入(“”)
文件。写入(“”)
文件。写入(“”)
document.write('row2')
文件。写入(“”)
文件。写入(“”)
文件。写入(“”)
文件。写入(“”)
文件。写入(“”)
文件。写入(“”)
document.write(“去掉
上的处理程序,并将
传递此
作为参数
document.write('<input type="button" value=" move UP " onClick="swapRowUp(this)"</input>')>
document.write('<input type="button" value="move DOWN" onClick="swapRowDown(this)"</input>')>
并让您的swapRowUp
和swapRowDown
函数调用getRowIndex
来获取索引
function swapRowUp( button ) {
var idx = getRowIndex( button );
}
function swapRowDown( button ) {
var idx = getRowIndex( button );
}
或者,如果必须直接从内联处理程序传递索引,只需将getRowIndex()
callinline
document.write('<input type="button" value=" move UP " onClick="swapRowUp(getRowIndex(this))"</input>')>
document.write('<input type="button" value="move DOWN" onClick="swapRowDown(getRowIndex(this))"</input>')>
我正在使用两个JS方法moveUp和moveDown,并通过上下两个按钮进行关联。单击up,我需要获取行的rowIndex并将其发送给moveUpmethod@nithin:我不会猜你的代码是什么样子的。请编辑你的问题,以包括回答你的问题所需的所有相关详细信息,包括实际HTML的表示形式结构,我会更新我的答案。我已经编辑了q'n以满足实际需要,很抱歉给您带来不便。我尝试使用ur逻辑,但无法执行交换操作,我得到的行索引为0>我已经发布了我使用的完整代码
document.write('<input type="button" value=" move UP " onClick="swapRowUp(getRowIndex(this))"</input>')>
document.write('<input type="button" value="move DOWN" onClick="swapRowDown(getRowIndex(this))"</input>')>
function swapRowUp( idx ) {
alert( idx );
}
function swapRowDown( idx ) {
alert( idx );
}