Home > Web Front-end > JS Tutorial > Example code sharing of how to get the selected value of checkbox in gridview using JavaScript

Example code sharing of how to get the selected value of checkbox in gridview using JavaScript

黄舟
Release: 2017-07-24 15:52:49
Original
1945 people have browsed it

This article mainly introduces js to get the value of the checkbox selected in the gridview. This article will share with you two code snippets, which are very good and have reference value. Friends in need can refer to it

Test well It took a long time to come out. The checkbox is in the first column,


 for( i=1;i<document.all.GVmain.rows.length;i++)
     {
      var cb=document.all.GVmain.rows(i).cells(0).children(0);
      if(cb.checked)
      {
         temp0=document.all.GVmain.rows(i).cells(1).innerText;
         temp1=document.all.GVmain.rows(i).cells(5).innerText;
         temp2=document.all.GVmain.rows(i).cells(6).innerText;
       }
     }
Copy after login

Okay, let me share with you a relatively good code snippet. The specific code is as follows:


///功能:判断是否有选中项;      
///参数:frm - 当前表单Form;idVal - 要查找的CheckBox的id;      
///返回:True/False;      
///调用:<INPUT type="submit" value="提交" id="btnS" runat="server"                  
onclick="JavaScript:return confirmSel(this.form, &#39;chkSel&#39;);">      
///说明:&#39;chkSel&#39; - 将判断所有id包含&#39;chkSel&#39;的控件;      
function confirmSel(frm, idVal)      
{            
// loop through all elements        
var IsChecked;        
IsChecked=false;        
for (i=0; i<frm.length; i++)        
{          
// Look for our checkboxes only         
if (frm.elements[i].id.indexOf (idVal) !=-1)           
{                   
// If any are checked then confirm alert, otherwise nothing happens            
if(frm.elements[i].checked)            
{              
IsChecked=true;             
return true;              
//return confirm (&#39;确定要提交所选择的记录吗?&#39;)            
}                   
}        
}             
if(IsChecked==false)       
{          
alert(&#39;请选择要进行操作的行!!!&#39;);         
return false;                 
}  
}            
///功能:对CheckBox实现单选功能;      
///参数:frm - 当前表单Form;chkVal - 当前CheckBox状态:选中True,不选中False;idVal - 当前CheckBox的id;     
///返回:True/False;      
///调用:<input type="checkbox" id=&#39;chkSel&#39; onpropertychange=&#39;JavaScript:selChk(this.form,this.checked,this.id);&#39; title="" runat="Server">           
///说明:hdnChkID - Hidden隐藏框,用于存放上次选中项CheckBox的id;      
function selChk(frm,chkVal,idVal)     
{                    if(chkVal == true) //如果当前CheckBox被选中        
{               
//将上次选中的CheckBox的id赋给变量lstChkID               
var lstChkID = document.getElementByIdx("hdnChkID").value;          
//记录当前CheckBox的id          
document.getElementByIdx("hdnChkID").value = idVal;          
if(lstChkID!=&#39;&#39;)        
{            
document.getElementByIdx(lstChkID).checked = false;         
}     
}                
} 
//CheckBox单选-遍历实现 
function singleSelCHK(idVal,chkId)
{ var vInput=document.getElementsByTagName_r("INPUT"); 
for(var i in vInput) 
{ var obj=vInput[i]; if(obj.type=="checkbox" && obj.id.indexOf(chkId)!=-1 && obj.id!=document.getElementByIdx(idVal).id)
{ 
//alert(obj.type); vInput[i].checked=false; } } }
Copy after login

Summary

The above is the detailed content of Example code sharing of how to get the selected value of checkbox in gridview using JavaScript. 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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template