Home  >  Article  >  Backend Development  >  Study notes for javascript all selection effect_PHP tutorial

Study notes for javascript all selection effect_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:451064browse

The effect I want to do is to automatically select all checkboxes when we open the page. The principle is: first get all the elements of type 'input', and then add a click event to each selected option. If all options are selected, the 'checked' attribute of each element becomes true through looping, otherwise it becomes false.

document.getElementById(' ') is the id attribute of an element to get an The specified element is returned as an object. document.getElementById(' ') obtains the control object based on the control TAG (type attribute) and returns an object array;

To achieve a select-all effect, you must first define a set of options.

The code is as follows Copy code
 代码如下 复制代码


 

全选


    

    

    

    

    

    

    

 


Select all
















Next use Javascript to achieve the effect. The effect in the tutorial is that you can only select all when you click Select All. If you click Select All again, you cannot deselect them all. Here I have added this function through my own understanding
 代码如下 复制代码

The code is as follows Copy code

Personally, I feel that this method is still lacking. It will select all input types on the page. Now we have written a method that uses click events to select all and inverse the selection effect.

In this way, if we combine it with the PHP program to realize the deletion function, we only need to provide the obtained string in the post or get method. Let's look at the get method.
The code is as follows
 代码如下 复制代码




1





Copy code

 代码如下 复制代码

//批量删除   
//na 是name  
function checkSubmit(na,url)
{
 
    var str = '';
    for(var i=0;i < document.getElementsByName(na).length;i++)
{
if(document.getElementsByName(na)[i].checked){
if(str=='') str += document.getElementsByName(na)[i].value;
else str += ',' + document.getElementsByName(na)[i].value;
}
}
if(str=='')
{
alert('你没选择任何内容!');
return false;
}
else
{
location=url+"?id="+str+"&action=delall";
}
}




1





 代码如下 复制代码

$a = $_GET['id'];

The code is as follows Copy code
//Batch deletion
//na is name
function checkSubmit(na,url)
{

var str = '';
for(var i=0;i < document.getElementsByName(na).length;i++)
{
If(document.getElementsByName(na)[i].checked){
If(str=='') str += document.getElementsByName(na)[i].value;
                else str += ',' + document.getElementsByName(na)[i].value;
                                                                                            }  
If(str=='')
{
alert('You have not selected anything!');
         return false;
}  
else
{
location=url+"?id="+str+"&action=delall";
}  
} In the php program we need to use
The code is as follows Copy code
$a = $_GET['id']; Then use where id in ( $a ) to delete it. This is also a simple php tutorial. Friends in need can learn and communicate.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628796.htmlTechArticleThe effect I want to do is to automatically select all checkboxes when we open the page. The principle is: first get all Elements with type 'input', and then add click events for each option that selects all...
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