Home  >  Article  >  Web Front-end  >  How to delete information in javascript

How to delete information in javascript

藏色散人
藏色散人Original
2021-07-02 14:45:112175browse

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}" />

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

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";
}

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!

Statement:
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