首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > ASP >

循环里同名checkbox怎么限定选取个数

2012-02-21 
循环里同名checkbox如何限定选取个数%i0DoWhileNotRs.Eofii+1%TABLETBODYtrtdheight 30 i

循环里同名checkbox如何限定选取个数
<%

i=0
Do   While   Not   Rs.Eof
i=i+1
%>
<TABLE>
  <TBODY>
    <tr>
      <td   height= "30 "> <input   type= "hidden "   name= "tid "   value= " <%=rs( "id ")%> "   > <%=i%> 、 <B> <FONT   class=title1> <%=rs( "title ")%>   </FONT> </B>   </td>
            </tr>
      <TR>
          <TD> <TABLE>
              <TBODY>
<%   sql2= "select   *   from   yh "
                      Set   Rs2=Server.CreateObject( "ADODB.RecordSet ")
      Rs2.Open   sql2,Conn,1,1
  %>
                    <tr>
                      <TD   vAlign=bottom>
                          <INPUT     type= " <%=lx%> "   name=uid     onClick= "doCheck(this) "   value= <%=rs2( "uname ")%> >
                          <LABEL> <%=rs2( "uname ")%> </LABEL> </TD>                  
                      </tr>
                      <%
                      Rs2.MoveNext
                      Loop%>      
              </TBODY>
<%   rs2.close
    set   rs2=nothing
%>
          </TABLE> </TD>
        </TR>
      </TBODY>
    </TABLE>

<%
Rs.MoveNext
Loop%>


[解决办法]
<script>
function chk(obj1){
var obj = document.forms[0];
var max = 5;
var j = 0;
if (obj.s.length){
for (i = 0; i < obj.s.length; i++){
if (obj.s[i].checked) j++;
if (j > max){
alert( "对不起,最大选取 " + max + "。 ");
obj1.checked = false;
break;
}
}
}
}
</script>
<form>
<input type= "checkbox " name= "s " value= "1 " onclick= "chk(this); " />
<input type= "checkbox " name= "s " value= "2 " onclick= "chk(this); " />
<input type= "checkbox " name= "s " value= "3 " onclick= "chk(this); " />
<input type= "checkbox " name= "s " value= "4 " onclick= "chk(this); " />
<input type= "checkbox " name= "s " value= "5 " onclick= "chk(this); " />


<input type= "checkbox " name= "s " value= "6 " onclick= "chk(this); " />
<input type= "checkbox " name= "s " value= "7 " onclick= "chk(this); " />
</form>

[解决办法]
哦,知道了.

把max和chekbox的name提出做为参数.

然后..循环出问题及答案时,chekbox的name变化即可.


<script>
function chk(obj1, max){
var obj = eval(obj1.name);
var j = 0;
if (obj.length){
for (i = 0; i < obj.length; i++){
if (obj[i].checked) j++;
if (j > max){
alert( "对不起,最大选取 " + max + "。 ");
obj1.checked = false;
break;
}
}
}
}
</script>
<input type= "checkbox " name= "s1 " value= "1 " onclick= "chk(this, 3); " />
<input type= "checkbox " name= "s1 " value= "2 " onclick= "chk(this, 3); " />
<input type= "checkbox " name= "s1 " value= "3 " onclick= "chk(this, 3); " />
<input type= "checkbox " name= "s1 " value= "4 " onclick= "chk(this, 3); " />
<hr/>
<input type= "checkbox " name= "s2 " value= "1 " onclick= "chk(this, 2); " />
<input type= "checkbox " name= "s2 " value= "2 " onclick= "chk(this, 2); " />
<input type= "checkbox " name= "s2 " value= "3 " onclick= "chk(this, 2); " />
<input type= "checkbox " name= "s2 " value= "4 " onclick= "chk(this, 2); " />

热点排行