2011-08-26 144 views
1

我有这个函数可以将带有可编辑类的单元格(td.editable)转换为输入字段。现在,您必须将该行传递给该函数。例如:editRow(myRow)如何传递一个变量作为函数参数

所以使用this.parentNode.id,我得到该行的id。但是当我将该值作为参数传递给editRow函数时,它什么都不做。

我认为函数认为rowId是行的实际名称,而不是使用rowId变量的内容作为行的名称。

function editRow(row) { 
$('td.editable',row).each(function() { 
    $(this).html('<input type="text" value="' + $(this).html() + '" size="10" />'); 
}); 
}  

/* click on cell with .editable class */ 
$(".editable").click(function() { 
    rowId = this.parentNode.id; 
    editRow(rowId); 
}); 

回答

4

editRow不占用行ID,它占用整行(DOM元素本身)。你只是传递错误的数据。尝试:

$('.editable').click(function(){ 
    editRow(this.parentNode); 
}); 
+0

+1 - 打我吧:-) –

+0

是的。 ['jQuery(selector,[context])'](http://api.jquery.com/jQuery/) - “** context ** - DOM Element,Document或jQuery用作上下文” – Wiseguy

+0

谢谢全部为您的解决方案。它现在很好用!我现在只有2个问题。 1)如果我点击该行,它按预期工作。但是,如果我点击另一行,它会进入编辑模式,但我首先点击的行也保持编辑模式,所以现在我在编辑中得到了两个或更多行。我试过'返回false',但它没有帮助。 2)如果我点击同一行两次或更多次,它会一直添加输入字段。 有人可以帮我这些吗? – leonel

0

鉴于你在这里的功能:

/* click on cell with .editable class */ 
$(".editable").click(function() { 
rowId = this.parentNode.id; 
editRow(rowId); 
}); 

你传入的ID您editRow功能。所以里面editRow,你可以这样做:

function editRow(row) { 
    $('#' + row +' td.editable').each(function() { 
     $(this).html('<input type="text" value="' + $(this).html() + '" size="10" />'); 
    }); 
} 

'#' 是jQuery的ID选择

相关问题