Examples to explain jQuery's implementation of checkbox instant modification and batch deletion functions

小云云
Release: 2017-12-22 10:28:35
Original
1951 people have browsed it

This article mainly introduces jQuery's implementation of checkbox instant modification and batch deletion and the pitfalls encountered in the process. It is very good and has reference value. Friends in need can refer to it. I hope it can help everyone learn and master jQuery better.

Recently I want to use jQuery to implement a batch deletion operation. The effect is as follows

The final page page.html, this page uses bootstrap and jQuery, if not Need to download


    视频管理      

视频管理

批量删除 0

删除成功

添加

选择 序号 名称 介绍 讲师 时长 播放次数 编辑 删除
1 环境搭建 这视频真tm牛逼 老王 666 666 编辑 删除
1 环境搭建 这视频真tm牛逼 老王 666 666 编辑 删除
1 环境搭建 这视频真tm牛逼 老王 666 666 编辑 删除

Copy after login

The operation in the above picture mainly requires two operations:

1. Count the selections when the checkBox is clicked (With pit).

2. When batch deletion is clicked, the ID of the selected unit is spliced and passed to a background.

At first glance, I feel that these are easy to implement, and there are many ways to implement them. I thought so too at first, but I spent the whole morning... Next, let's take a look at some of the pitfalls of jQuery.

My initial idea is to perform each() traversal on all checkBoxes every time the checkBox is clicked. If it is checked, add Num++, and finally assign the value of num to the small digital labels for batch deletion. By the way The ids are also spliced.


$(".check_0").click(function(){ var num=0; var del_str=""; $(".check_0").each(function(){ alert($(this).val()+":"+$(this).attr("checked")); if($(this).attr("checked")=="checked"){ num++; del_str+=$(this).parent().siblings("show_id").html()+"/"; } alert(this.checked); }) })
Copy after login

When this code is triggered, a strange phenomenon is discovered: this code is triggered before the effect is checked. Similar to beforeClick(), so when the code is traversed, the current check status cannot be obtained.

Regarding this problem, I searched for the answer on the Internet for a long time, but I just didn’t know how to solve it. I tried all kinds of mouseup(), and they all had the effect of beforeClick.

Finally I used another way of writing.


//获取选中的个数 $(".check_0").click(function() { $("#badge_0").html($("input[type=checkbox]:checked").length); //alert($("input[type = checkbox]: checked ").length); }) //批量删除 $("#batchDel").click(function() { var params = ""; $("input[type = checkbox]:checked").each(function(index,element){ //第一个id不需要加前缀 if(index == 0) { params += "id=" + $(this).val(); } else { params += "&id=" + $(this).val(); } }); alert("生成的拼接参数:" + params); })
Copy after login

The effect was successfully run!

Conclusion:

When using theinput[type=[checkbox]:checked]selector in click() to get the checked element, what you get is the checked element. The result of afterclick().

When using .each() to traverse, you get the effect before clicking.

Why can there be two effects, beforeclick and aferclick, in a click function?

Related recommendations:

jQuery implements the search function and displays search-related content

Detailed explanation jQuery clicks anywhere except the specified area to hide the DIV Function

Practical exampleDetailed explanation of the simple operation of jQuery to implement checkbox

The above is the detailed content of Examples to explain jQuery's implementation of checkbox instant modification and batch deletion functions. 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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!