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; } }
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, 'chkSel');"> ///说明:'chkSel' - 将判断所有id包含'chkSel'的控件; 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 ('确定要提交所选择的记录吗?') } } } if(IsChecked==false) { alert('请选择要进行操作的行!!!'); return false; } } ///功能:对CheckBox实现单选功能; ///参数:frm - 当前表单Form;chkVal - 当前CheckBox状态:选中True,不选中False;idVal - 当前CheckBox的id; ///返回:True/False; ///调用:<input type="checkbox" id='chkSel' onpropertychange='JavaScript:selChk(this.form,this.checked,this.id);' 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!='') { 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; } } }
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!