您好,登錄后才能下訂單哦!
jQurey代碼部分:
- <script type="text/javascript">
- var flag = 0;
- //添加新行
- function addRow() {
- var nrow = "<tr><td><input name='hideid' type='hidden' value='' /><input name='username' type='text' value='' size='15' maxlength='15' /></td><td><select name='seltype' id=seltype1><option value='Text'>文本框</option><option value='textarea'>多行文本區</option><option value='Select'>下拉框</option><option value='Radio'>單選框</option><option value='Checkbox'>復選框</option></select></td><td><span class='heb' name='buttonspan'><input value='添加' onclick='addData(this)' type='button' class='se_buton'/> <input value='取消' type='button' onclick='deleteNewRow(this)' class='se_buton'/></span></td></tr>";
- if (flag == 0) {
- $('#attributetable').append(nrow);
- flag = 1;
- }
- }
- //添加新行中的數據到后臺
- function addData(obj) {
- var trobj = $(obj).parents('tr');
- var username = $(trobj).find(':text[name=username]').val();
- var seltype = $(trobj).find('select[name=seltype]').val();
- $.post("SupplyAJAX.aspx", { username: username, seltype: seltype, type: "add" }, function (data) {
- if (data.toString() != "0") {
- $(trobj).find(':hidden[name=hideid]').val(data.toString());
- changeDisable(obj, 0);
- }
- else {
- deleteNewRow(obj);
- alert('添加失敗');
- }
- });
- flag = 0;
- }
- //更新行
- function updData(obj) {
- var trobj = $(obj).parents('tr');
- var id = $(trobj).find(':hidden[name=hideid]').val();
- var username = $(trobj).find(':text[name=usrname]').val();
- var seltype = $(trobj).find('select[name=seltype]').val();
- $.post("SupplyAJAX.aspx", { id: id, username: username, seltype: seltype, type: "update" },
- function (data) {
- if (data.toString() != "0") {
- changeDisable(obj, 0);
- }
- else {
- alert('更新失敗');
- }
- });
- }
- //刪除新行
- function deleteNewRow(obj) {
- $(obj).parents('tr').replaceWith('');
- flag = 0;
- }
- //刪除數據庫中的行
- function deleteRow(obj) {
- var trobj = $(obj).parents('tr');
- var id = $(trobj).find(':hidden[name=hideid]').val();
- $.post("SupplyAJAX.aspx", { id: id, type: "delete" },
- function (data) {
- if (data.toString() != "0") {
- $(obj).parents('tr').replaceWith('');
- }
- else {
- alert('刪除失敗');
- }
- });
- }
- //改變編輯狀態
- function changeDisable(obj, type) {
- var trobj = $(obj).parents('tr');
- if (type == 0) {
- $(trobj).find(':text').attr('disabled', 'disabled');
- $(trobj).find('select').attr('disabled', 'disabled');
- $(trobj).find('span[name=buttonspan]').html("<input name='gx' value='更新' type='button' onclick='changeDisable(this,1)' class='se_buton'/> <input value='刪除' type='button' name='qx' onclick='deleteRow(this)' class='se_buton' />");
- }
- if (type == 1) {
- $(trobj).find(':text').attr('disabled', '');
- $(trobj).find('select').attr('disabled', '');
- $(trobj).find('span[name=buttonspan]').html("<input name='gx' value='確定' type='button' onclick='updData(this)' class='se_buton'/> <input value='取消' type='button' name='qx' onclick='changeDisable(this,0)' class='se_buton' />");
- }
- }
- </script>
HTML代碼部分:
- <body>
- <table id="attributetable">
- </table>
- <div class=" heb"><input value="增加一行" type="button" onclick='addRow()' class="se_buton"/> </div>
- </body>
SupplyAJAX類
- public partial class SupplyAJAX : System.Web.UI.Page
- {
- static List<UserModel> UmList = new List<UserModel>();
- protected void Page_Load(object sender, EventArgs e)
- {
- switch (Request.QueryString["type"])
- {
- case "add":
- Add();
- break;
- case "update":
- Update();
- break;
- case "delete":
- Delete();
- break;
- }
- Response.End();
- }
- private string Add()
- {
- UserModel um = new UserModel();
- um.Id = UmList.Count + 1; //自動為ID加1
- um.Username = Request.Form["username"];
- um.Seltype = Request.Form["seltype"];
- UmList.Add(um);
- return um.Id.ToString();
- }
- private string Update()
- {
- int id = int.Parse(Request.QueryString["id"]);
- for (int i = 0; i < UmList.Count; i++)
- {
- if (id == UmList[i].Id)
- {
- UmList[i].Username = Request.Form["username"];
- UmList[i].Seltype = Request.Form["seltype"];
- return UmList[i].Id.ToString();
- }
- }
- return "0";
- }
- private string Delete()
- {
- int id = int.Parse(Request.Form["id"]);
- for (int i = 0; i < UmList.Count; i++)
- {
- if (id == UmList[i].Id)
- {
- UmList.Remove(UmList[i]);
- return UmList[i].Id.ToString();
- }
- }
- return "0";
- }
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。