Jquery在函数之间传递表行

时间:2012-09-13 19:54:17

标签: javascript jquery crud

我使用jQuery编写了一个简单的网格布局。我有一个问题。我想一次只允许一个编辑行。因此,我会跟踪当前的编辑行,并在单击下一个编辑按钮时将其重置。当我下次点击编辑时,它允许我编辑。 completeEdit工作正常,因为我也通过传递当前行从取消按钮调用它。

 var currentRowEdit =null;

   $(tableid + ".edit").live('click', function(event) {
            currentRowEdit = $(this).parent().parent();
            editRow(currentRowEdit);
    });


function editRow(row){
        if(currentRowEdit!=null){
            completeEdit(currentRowEdit);               
        }              
           $(row).find(".save").show();
           $(row).find(".cancel").show();
           $(row).find(".edit").hide();

    }

function completeEdit(row){             
         $(row).find(".save").hide();
          $(row).find(".cancel").hide();
         $(row).find(".edit").show();              
    }

2 个答案:

答案 0 :(得分:1)

function completeEdit(row){
   $(row).find(".save").hide();
   $(row).find(".cancel").hide();
   $(row).find(".edit").show();   
   currentRowEdit = null; // reset the currentRowEdit
}

根据评论

您的代码未隐藏.save.cancel按钮。因为,如果你看一下你的代码流会注意到,completeEdit()函数之后你正在使用.show()方法来获取这些按钮。

因此,您的按钮会在completeEdit()完成后隐藏,但在此之后执行editRow()的其余代码时,这些按钮将再次显示。

答案 1 :(得分:0)

我相信你在同一行调用了completeEdit和editRow。你应该在之前调用completeEdit并在新的

上调用editRow
 var currentRowEdit =null;

   $(tableid + ".edit").live('click', function(event) {
            completeEdit(currentRowEdit);
            currentRowEdit = $(this).parent().parent();
            editRow(currentRowEdit);
    });


function editRow(row){



           $(row).find(".save").show();
           $(row).find(".cancel").show();
           $(row).find(".edit").hide();

    }

function completeEdit(row){
         if(row==null){
           return;

        }
         $(row).find(".save").hide();
          $(row).find(".cancel").hide();
         $(row).find(".edit").show();    

    }