当前位置: 代码迷 >> Java Web开发 >> selectBooleanCheckbox全选有关问题
  详细解决方案

selectBooleanCheckbox全选有关问题

热度:61   发布时间:2016-04-17 13:11:00.0
selectBooleanCheckbox全选问题
table的行头有一个selectBooleanCheckbox,点它会全选/全不选下面的所有selectBooleanCheckbox,请问怎么做   谢谢了

------解决方案--------------------
<script language= "javascript ">
function del()//用于判断记录有没有选中的函数
{
var flag=true;//用于存储有没有选择的变量,false为有选,true没选
var temp= " ";//复选框的值,并事先给它预赋值为空,否则它的值为未定义
var tmp;//复选框数
if((document.form1.answer.length)== "undefined ") //判断复选框的数目,当只有1个复选框时不能使用复选框length属性,所以它的值为undefined
{
tmp=1
}
else
{
tmp=document.form1.answer.length
}
//接下来就分两种情况来处理,一种是只有1个复选框,另一种是多个复选框
if(tmp==1)
{
if(document.form1.answer.checked) //当复选框被选中时
{
flag=false;
temp=document.form1.answer.value //因为只有1个复选框,所以将值直接付给temp
}
}
else //另外一种情况有多个复选框时
{
for(i=0;i <document.form1.answer.length;i++) //for循环
{
if(document.form1.answer[i].checked) //当answer[i]被选中时为true
{
if(temp== " ")
{
flag=false;
temp=document.form1.answer[i].value
}
else
{
flag=false;
temp=temp+ ", "+document.form1.answer[i].value
}
}
}
}
if(flag)
{
alert( "对不起,你还没选择! ");
}
else
{
//name=document.form1.name.value
//alert(name)
if(confirm( "确实要删除? "))
{
//window.location= "delnews.asp?id= "+temp;//发送id号到删除处理页
alert( "删除ID为 "+temp);
}
}
//return !flag;
}

//全选函数
function checkall(all)//用于判断全选记录的函数
{
var a=document.getElementsByName( "answer ");//返回answer的集合
for(var i=0; i <a.length;i++)a[i].checked=all.check1.checked;
}

</script>
<form name= "form1 " method= "post " action= " ">
<input type= "checkbox " name= "answer " value= "1 "> 1 <br>
<input type= "checkbox " name= "answer " value= "2 "> 2 <br>
<input type= "checkbox " name= "answer " value= "3 "> 3 <br>
<input type= "checkbox " name= "answer " value= "4 "> 4 <br> 全选
<input type= "checkbox " name= "check1 " onclick= "checkall(form1) "> <br>
<input type= "button " name= "b1 " value= "删除 " onClick= "del() ">
</form>


------解决方案--------------------
//全选or全不选
function selectAll()
{
var oTable=document.all[ 'form:dataTable '];
var oChkAll=document.all[ 'form:selectall ']

if(oTable != null && oChkAll != null)
{
for(j=1;j <oTable.rows.length;j++)
{
oTable.rows(j).cells(0).children.item(0).checked=oChkAll.checked;
}
}
}

JSF页面代码:
...
<h:selectBooleanCheckbox id= "selectall " onclick= "selectAll() " />
<h:outputLabel for= "selectall ">
<h:outputText value= "全选 " />
</h:outputLabel>

<t:dataTable align= "left " border= "1 " rows= "10 " first= "0 "
id= "dataTable " rendered= "true "
value= "#{empController.empModel} " var= "emp ">
<t:column>
<f:facet name= "header ">
<h:outputText value= "Select "> </h:outputText>
  相关解决方案