jquery 編輯table tr

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">

<!-- saved from url=(0059)http://www.360doc.com/content/06/1229/11/16915_311738.shtml -->
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD>
<TITLE>js操作刷新</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8";>
<META content=360doc name=classification>
<META content=js操作刷新 name=keywords>
<META content=js操作刷新 name=description>
<script src="jquery.js" type="text/javascript"></script>
<body>
<h1>實例1</h1>
<table cellpadding="2" cellspacing="2" border="1" width="80%">
<tr>
<td>所屬類別</td>
<td>最小值</td>
<td>最大值</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>類別1</td>
<td>2</td>
<td>10</td>
<td id="1"><input type="button" onClick="edit(this)" value="修改"/></td>
</tr>
<tr>
<td>類別2</td>
<td>5</td>
<td>100</td>
<td id="2"><input type="button" onClick="edit(this)" value="修改"/></td>
</tr>
</table>

<script type="text/javascript">
function edit(obj){
    var old0=$(obj).parent().prevAll(":eq(0)").text();
    var old1=$(obj).parent().prevAll(":eq(1)").text();
    $(obj).parent().prevAll(":eq(0)").data("oldtxt",old0);//將單元格原文本保存在其緩存中,便修改失敗或取消時用
    $(obj).parent().prevAll(":eq(1)").data("oldtxt",old1);
    $(obj).parent().prevAll(":eq(0)").html("<input type='text' value='"+old0+"'");
    $(obj).parent().prevAll(":eq(1)").html("<input type='text' value='"+old1+"'");
    $(obj).parent().append("&nbsp;<input type='button' onClick='cancel(this)' value='取消'/>");
    $(obj).attr("value","保存");
    $(obj).attr("onClick","save(this)");
}

function save(obj){
    var id=$(obj).parent().attr("id");  //id
    var category=$(obj).parent().prevAll(":eq(2)").text(); //類別
    var min=$(obj).parent().prevAll(":eq(1)").children().val().trim(); //默認值
    var max=$(obj).parent().prevAll(":eq(0)").children().val().trim(); //最大值
    jQuery.ajax({
        type : 'POST',
        url     : '<%=basePath%>/test.do?method=modify',
        data : 'id='+id+'&category='+category+'&min='+min+'&max='+max,
        dataType : 'json',
        success : function(rs){
            if(rs.success){
                $(obj).parent().prevAll(":eq(1)").html(min);
                $(obj).parent().prevAll(":eq(0)").html(max);
                $(obj).parent().html("<input type='button' onClick='edit(this)' value='修改'/>");
            }else{
                jAlert(errorTitle,'失敗',true);
                cancel(obj); //取消
            }
        }
    });
}

function cancel(obj){
    var old0=$(obj).parent().prevAll(":eq(0)").data("oldtxt");//將單元格內容設爲原始數據
    var old1=$(obj).parent().prevAll(":eq(1)").data("oldtxt");
    $(obj).parent().prevAll(":eq(0)").html(old0);
    $(obj).parent().prevAll(":eq(1)").html(old1);
    $(obj).parent().html("<input type='button' onClick='edit(this)' value='修改'/>");
}
</script>
</body></html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章