Home > Web Front-end > JS Tutorial > body text

How to delete information in javascript

藏色散人
Release: 2021-07-02 14:45:11
Original
2182 people have browsed it

How to delete information in javascript: first add a field in front of each piece of data; then click the batch delete button to jump to the batchDelete() method; and finally create the background processing code.

How to delete information in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to delete information with javascript?

JS Ajax batch deletion of data information

Batch deletion of data information is already a very common functional requirement. The following is a JS Ajax submission to the background for data processing A small example of deletion:

First add a field in front of each piece of data to display a check box. The code is as follows:

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

Click the batch delete button to jump to batchDelete() Method: The code is as follows:

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("刪除失败!")
            }
        });
    }
}
Copy after login

The background Java processing code is as follows:

@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";
}
Copy after login

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to delete information in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template