重要步驟就省略了………………
1、刪除實現無刷新操作
$(function () {
$(".delroom").live("click", function () {
var that = this;
var rid = $(this).attr("ref");
$.post("cuisine_a.aspx?action=delroom", { DishSortID: rid }, function (r) { //post傳值,想該頁面發送數據DishSortID,發送成功時回調函數function(r)
if (r == "True") {
VMsg.AlertSuccess("刪除成功!");
$(that).parents("tr").remove();
}
else {
}
}, "text");
});
});
$('table:not(.edit) tr:not([th]):even').addClass('even');
$('table:not(.edit) tr:not([th]):odd').addClass('odd');
});
獲得 test.php 頁面的內容,並存儲爲 XMLHttpResponse 對象,並通過 process() 這個 JavaScript 函數進行處理:
jQuery 代碼:
$.post("test.php", { name: "John", time: "2pm" },
function(data){
process(data);
}, "xml");
<a class="operating operating_yellow delroom" href="#" ref="<%# Eval("DishSortID") %>"><img src="/images/a_call_delete.png">刪除</a>
前臺用jquery post傳值後,後臺用request[“action”]和Request["DishSortID"]來獲取post傳過去的值,然後在後臺寫相關的刪除和編輯方法,此方法會用到sql語句對數據庫進行相關操作
if (Request["action"].ToLower() == "delroom") DelRoom("sort");
/// <summary>
/// 刪除單個菜系
/// </summary>
void DelRoom(string type)
{
int DishSortID = 0;
int ShopID = CurrentUser.ShopID;
int AppID = CurrentUser.AppID;
if (int.TryParse(Request["DishSortID"], out DishSortID))
{
if (DishTagDal.existTag(ShopID, AppID, type, DishSortID))
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('此分類下已經有菜品,禁止刪除!');", true);
}
else Output(DishTagDal.DelRoom(type, CurrentUser.AppID, CurrentUser.ShopID, DishSortID) ? "True" : "False");
}
}
2、如果編輯的話,需要涉及到傳值問題,本頁面會有個彈出層,然後從表中取值,
$(".editroom").live("click", function () {
var roomid = $(this).attr("ref"); //返回屬性值
var tds = $(this).parents("tr").find("td"); //獲取tr的父元素,然後找td
var name = tds.eq(2).text(); //獲取索引值index爲2的文本值
var imgpath = tds.eq(1).find("img").attr("src");//獲取索引值爲1,然後找到img,獲取img的src屬性值
var ImgName = tds.eq(1).find("img").attr("ref");//獲取索引值爲1,然後找到img,獲取img的ref屬性值
var p = tds.eq(0).text();
$("#img_id").attr("src", imgpath); //從這開始,對彈出框開始賦值了,取id爲img_id的img,然後設置其src的屬性值爲imgpath,這裏imgpath上面已經獲取到值了
$("#Img_url").val(ImgName);//value值爲imgName
$("#cuisine_name").val(name);
$("#OrderID").val(p);
artDialog({//彈出層相關設置
content: $("#SaveRoom")[0],//彈出框內容爲id爲SaveRoom的div
title: "編輯分類",
okVal: "保存",
ok: function () {//點擊確定按鈕,執行下面操作
SaveRoom("edit", roomid);//單獨把這個方法寫出來
return false;
},
cancelVal: '關閉',
cancel: true,
lock: true
});
});
function SaveRoom(action, rid) {
VerifyByPoshyTip.init([//驗證狀態,是否符合標準,然後才能對數據庫操作
{ 'ContentID': 'cuisine_name', 'AimID': 'cuisine_name', 'TipContent': '名稱最多可以輸入16個字符,請修改!', 'fn': function () { return $("#cuisine_name").val().Trim().checkScope(1, 16); } },
{ 'ContentID': 'Img_Tip', 'AimID': 'Img_Tip', 'TipContent': '請您上傳正確的圖片!', 'fn': function () { return $("#Img_url").val().Trim() != ""; } },
{ 'ContentID': 'OrderID', 'AimID': 'OrderID', 'TipContent': '請您輸入正確的數字!', 'fn': function () { return $("#OrderID").val().isInt(); } }
]);
if (!VerifyByPoshyTip.V()) return false;
var room_name = $("#cuisine_name").val();
//取值
var orderid = $("#OrderID").val(); //排序
var path = $("#Img_url").val();
if (room_name.length > 0) {//這裏條件貌似不夠,但是不影響操作
$.post("cuisine_a.aspx?action=" + action, { name:room_name, order: orderid, path:path, rid: rid }, function (r) {//這裏jQuery的post方法,相關原理可以百度,傳值過去,就要看後臺有沒有相應操作,從這句就應該進入後臺程序,相關程序執行完成後,會有一個output輸出,接着進行下面操作,如果輸出(返回)的是True,則接着下面的操作
if (r == "True") {
VMsg.Waiting({ time: 2, content: action == "edit" ? "修改成功" : "新增成功", icon: "succeed", close: function () {
location.reload();
}
});
} else {
VMsg.AlertError(action == "eidt" ? "您輸入的菜品分類已經存在,請重新輸入!" : "您輸入的菜品分類已經存在,請重新輸入!");
}
}, "text");
}
}
if (Request["action"].ToLower() == "delroom") DelRoom("tag");
else SaveRoom(sql);
/// 保存/修改標籤
/// </summary>
private void SaveRoom(string type)
{
string name = Request["name"].Trim();//標籤名稱
int ShopID = CurrentUser.ShopID;
int AppID = CurrentUser.AppID;
int roomid = 0;
int orderid = Convert.ToInt32(Request["order"]);
string action = Request["action"].ToString();
if (action == "edit")//如果是編輯,判斷是否與其他標籤名稱相同;如果添加,則判斷是否與所有標籤名稱相同
{
int TagID = Int32.TryParse(Request["rid"], out roomid) ? roomid : 0;//編輯,則可以得到主鍵
if (DishTagDal.existeditTag(TagID, ShopID, AppID, name)) return;//爲真,則表示與其他標籤名相同
}
else if (DishTagDal.existTag(ShopID, AppID, name))
{
return;
}
DishTag model = new DishTag()//用這種方式,把數據給model層
{
TagName = name,
TagType = type,
AppID = CurrentUser.AppID,
ShopID = CurrentUser.ShopID,
OrderID = orderid,
TagID = Int32.TryParse(Request["rid"], out roomid) ? roomid : 0//主鍵
};
if (string.IsNullOrEmpty(name)) Output(true, "", Context);
else
{
Output(DishTagDal.SaveTag(model).ToString());
}
}