JAVASCRITP checkbox Full selection of the reverse selection code

Source: Internet
Author: User
Tags eval

JAVASCRITP checkbox Full selection of the reverse selection code

Total selection and reverse selection

<script type= "text/web Effects" >
var Checkflag = "false";
function check (field)
{
if (Checkflag = = "false")
{
for (i = 0; i < field.length; i++)
{
field[i].checked = true;}
Checkflag = "true";
Return "Select All";
}
Else
{
for (i = 0; i < field.length; i++)
{
field[i].checked = false;
}
Checkflag = "false";
Return "Select All";
}
}
</script>
Add to
function Adddeptid (DeptID)
{
var dept_id = DeptID;
if (dept_id==0)
{
Alert ("Please select a department!") ")
Return
}
Else
{
var re=showmdialog (' emp_add.asp tutorial x?dept_id= ' +dept_id, ' 820 ', ' 670 ', ' no ');
if (re== "OK") {
Window.location.reload ();
}
}
}
Modify (Modify is to determine that checkbox can only select one to modify, and finally get the selected value to jump the page as a parameter)
function Checkboxnum (emp_id) {
CheckBox Selection
var DataList = document.getElementById ("GridView1");
var inputs = Datalist.getelementsbytagname ("input");
var checked_counts = 0;
var checkevalue = 0;
for (Var i=0;i<inputs.length;i++)
{
if (inputs[i].checked)
{
Checkevalue = Inputs[i].value;
checked_counts++; }
}
if (checked_counts>1)
{
Alert ("Only one item can be selected!") ");
}
else if (checked_counts==0)
{
Alert ("Please select a modification!") ");
}
Else
{
var re=showmdialog (' emp_add.aspx?edit_emp_id= ' +emp_id, ' 820 ', ' 670 ', ' no ');
if (re== "OK") {
Window.location.reload ();
}
}

Delete (first determine the number of checkbox selections, in the form of an array to wear to another page)
function Delselectedclick (emp_id)
{
if (emp_id==0)
{
Alert ("Please select a department!") ")
Return
}
var DataList = document.getElementById ("GridView1");
var Chkarray = datalist.getelementsbytagname ("input");
var flag = false;
var checkvalue = "";
for (Var i=0;i<chkarray.length;i++)
{
if (chkarray[i].type== "checkbox")
{
if (chkarray[i].checked)
{
CheckValue + = chkarray[i].value+ ",";
Flag = true;
}
}
}
if (flag = = True)
{
if (Confirm (OK to delete selected user?))
{
Window.navigate ("user_content.aspx?delete_emp_id=" +emp_id);
return true;
}
Else
{
return false;
}
}
Else
{
Alert ("Select the user you want to delete first!") ");
return false;
}

<table>
     <tr>
           <td>
                <input id= "CheckBox1" type= "checkbox"/> Select all
                 <a href= "Javascript:adddeptid (<%=ideptid%>)" style= "Cursor:pointer" > add </a>
                <a href= "Javascript:checkboxnum (<%=ideptid%>)" style= "Cursor:pointer" > Modify </a>
                 <a href= "Javascript:delselectedclick (<%=ideptid%>)" style= "Cursor:pointer" > Delete </a>

</td>
</tr>
</table>

Core code

/**
* Click method of checkbox
Requirements
* 1. The Name property of a checkbox that is selected/not selected is +_all for the Name property name of a single checkbox, note: The last must be _all.
* For example: The name=or_id of a single checkbox, the Name=or_id_all of all selected checkbox
* 2. Select/All optional checkbox You must define the id attribute, and the id attribute value is the same as the Name property value
* 3. All individual checkbox in the same form.
* 4. All select/All optional checkbox can also be outside form in form.
* 5. The entire page does not allow the checkbox to have the same name and the same ID component as the full selection/all option.
*
*/
function _tlscheckboxselall (formobj,checkboxclicked) {
var checkname = checkboxclicked.name;//Gets the Name property of a clicked checkbox
var index = checkname.indexof ("_all");//Determine if there is a _all string
var isall = false;
if (index!=-1) {//if _all is available
if (index+ "_all". Length==checkname.length) {//Satisfy description _all is on the last side, indicating that this is a checkbox with all/no selections.
Isall = true;
}
}
var thischecked = checkboxclicked.checked;//The status of the current checkbox true= selected False= not selected
if (Isall) {//click checkbox with all/no selections
var childcheckname = checkname.substring (0,index);
Eval ("var allchildcheckboxs = formobj.") +childcheckname);
var childcheckboxcount = allchildcheckboxs.length;
if (childcheckboxcount==null) {//only 1 single checkbox
allchildcheckboxs.checked = thischecked;
}else{//has 2 or more single checkbox of more than 2
for (Var i=0;i<childcheckboxcount;i++) {
allchildcheckboxs[i].checked = thischecked;
}
}
}else{//Point a single checkbox
var parentcheckboxname = checkname+ "_all";
if (Thischecked==false) {
document.getElementById (parentcheckboxname). Checked=false;
}else{//determines whether the entire single checkbox after the current checkbox is in the true state
Eval ("var allchildcheckboxs = formobj.") +checkname);
var childcheckboxcount = allchildcheckboxs.length;
var isallchecked = true;
if (childcheckboxcount==null) {//only 1 single checkbox
if (Allchildcheckboxs.checked==false) {
isallchecked = false;
}
}else{//has 2 or more single checkbox of more than 2
for (Var i=0;i<childcheckboxcount;i++) {
if (Allchildcheckboxs[i].checked==false) {
isallchecked = false;
Break
}
}
}
if (isallchecked) {//All single checkbox is in true state
document.getElementById (parentcheckboxname). Checked=true;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.