首頁  >  文章  >  web前端  >  javascript怎麼刪除訊息

javascript怎麼刪除訊息

藏色散人
藏色散人原創
2021-07-02 14:45:112118瀏覽

javascript刪除資訊的方法:先在每個資料的前面加一個欄位;然後點選批次刪除按鈕跳到batchDelete()方法;最後建立後台處理程式碼即可。

javascript怎麼刪除訊息

本文操作環境:windows7系統、javascript1.8.5版、Dell G3電腦。

javascript怎麼刪除資訊?

JS Ajax批量刪除資料資訊

批量刪除資料資訊已經是很常見的功能需求了,以下是一個JS Ajax提交給後台進行數據刪除的一個小例子:

首先在每個資料的前面加一個字段,用來顯示複選框,程式碼如下:

<input type="checkbox" name="userCheck" value="${user.id}" />

點擊批次刪除按鈕跳到batchDelete()方法:程式碼如下:

function batchDelete(){
    //判断至少选择了一项
    var checkedNum = $("input[name=&#39;userCheck&#39;]:checked").length;
    if (checkedNum == 0) {
        alert("至少选择一项删除!");
        return;
    }
    if (confirm("确定删除选中的用户?")) {
        var userList = new Array();
        $("input[name=&#39;userCheck&#39;]:checked").each(function(){
            userList.push($(this).val());
        });
    $.ajax({
            type : "post",
            url : "<%=request.getContextPath() %>/user/batchDelete",
            data : {userList : userList.toString()},
            success : function(){
                alert("删除成功!");
                location.reload();
            },
            error : function(){
                alert("刪除失败!")
            }
        });
    }
}

後台Java處理程式碼如下:

@RequestMapping(value="/batchDelete")
public String batchDelete(String userList){
    String[] strs = userList.split(",");
    for (int i = 0; i < strs.length; i++) {
        userService.delete(Integer.parseInt(strs[i])); 
    }
    return "redirect:/user/users";
}

推薦學習:《javascript進階教學

以上是javascript怎麼刪除訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn