Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将变量作为函数参数传递_Javascript_Html Table_Row_Edit - Fatal编程技术网

Javascript 如何将变量作为函数参数传递

Javascript 如何将变量作为函数参数传递,javascript,html-table,row,edit,Javascript,Html Table,Row,Edit,我有一个函数,可以将类为可编辑(td.editable)的单元格转换为输入字段。现在,您必须将该行传递给函数。例如:editRow(myRow) 因此,使用this.parentNode.id,我获得了行的id。但当我将该值作为参数传递给editRow函数时,它什么也不做 我认为函数认为rowId是行的实际名称,而不是使用rowId变量的内容作为行的名称 function editRow(row) { $('td.editable',row).each(function() { $(thi

我有一个函数,可以将类为可编辑(
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);
});
函数编辑行(行){
$('td.editable',行)。每个(函数(){
$(this.html(“”);
});
}    
/*单击带有可编辑类的单元格*/
$(“.editable”)。单击(函数(){
rowId=this.parentNode.id;
editRow(rowId);
});

editRow
不接受行id,而是接受整行(DOM元素本身)。你只是传递了错误的数据。尝试:

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

editRow
不接受行id,而是接受整行(DOM元素本身)。你只是传递了错误的数据。尝试:

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

鉴于您在这里的功能:

/* 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" />');
    });
}
函数编辑行(行){
$(“#”+行+“td.editable”)。每个(函数(){
$(this.html(“”);
});
}

“#”是jquery id选择器

给定您的函数:

/* 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" />');
    });
}
函数编辑行(行){
$(“#”+行+“td.editable”)。每个(函数(){
$(this.html(“”);
});
}

“#”是jquery id选择器

是的。-“上下文-用作上下文的DOM元素、文档或jQuery”感谢大家提供的解决方案。现在效果很好!我现在还有两个问题。1) 如果我单击该行,它将按预期工作。但是如果我在另一行中单击,它将进入编辑模式,但我首先单击的行也将保持编辑模式,因此现在我在“编辑更多”中有两行甚至更多行。我尝试了
返回false
,但没有效果。2) 如果我单击同一行两次或多次,它将继续添加输入字段。谁能帮我拿这些吗?是的“上下文-用作上下文的DOM元素、文档或jQuery”感谢大家提供的解决方案。现在效果很好!我现在还有两个问题。1) 如果我单击该行,它将按预期工作。但是如果我在另一行中单击,它将进入编辑模式,但我首先单击的行也将保持编辑模式,因此现在我在“编辑更多”中有两行甚至更多行。我尝试了
返回false
,但没有效果。2) 如果我单击同一行两次或多次,它将继续添加输入字段。有人能帮我拿这些吗?