thymeleaf+js实现单个删除和多选删除的功能

来源:互联网 发布:批处理 base64源码 编辑:程序博客网 时间:2024/06/16 00:22

在删除前我们需要给用户提示,根据用户选择来进行判断是否删除,当onclick和href同时使用的时候,会先执行onclick,根据onclick返回值及逆行判断是否执行href链接。

此处给出示例代码:

--------------------------------------------------------------------------------------------------------------------------------------

单个删除(id的值为后台传过来的值):

<a class="button border-red" href="#" th:href="@{/hr/del/{id}(id=${position.posno})}"                   onclick="return del()" th:value="${position.posno}" value="1">                    <span class="icon-trash-o"></span> 删除</a>

js:

function del() {        if (confirm("您确定要删除吗?")) {            return true;        } else {            return false;        }}

--------------------------------------------------------------------------------------------------------------------------------------

多选删除

多选框checkbox:

    <td style="text-align:left; padding-left:20px;">            <input type="checkbox" name="id[]" value="" id="id[]" th:value="${position.posno}"/>     </td>

删除按钮:

<button class="button border-red" onclick="DelSelect()"><span class="icon-trash-o"></span> 批量删除</button>



js代码:

function DelSelect() {        var Checkbox = false;        $("input[name='id[]']").each(function() {            if (this.checked == true) {                Checkbox = true;            }        });        if (Checkbox) {            var t = confirm("您确认要删除选中的内容吗?");            if (t == false) {                return false;            } else {                var checkedID="";                $("input[name='id[]']").each(function() {                    if (this.checked == true) {                        checkedID += this.value + ",";                    }                });                window.location.replace("/hr/delSelected?checkedID="+checkedID);            }        } else {            alert("请选择您要删除的内容!");            return false;        }    }



原创粉丝点击