jquery 編輯table tr

來源:互聯網
上載者:User

最近因項目需要整理了個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>


摘自 spring_0534的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.