⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkbox的jsp应用.htm

📁 有关JSP的技巧
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0074)http://www.javaresearch.org/article/showarticle.jsp?column=331&thread=6069 -->
<HTML><HEAD><TITLE>Java 研究组织 - 精品文章</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312"><LINK 
href="checkbox的jsp应用.files/zaxis.css" rel=stylesheet>
<SCRIPT language=JavaScript><!--function SymError(){  return true;}window.onerror = SymError;//--></SCRIPT>

<SCRIPT language=JavaScript src=""></SCRIPT>

<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY text=#000000 bgColor=#f1f1f1 leftMargin=0 topMargin=2><!-- start of header row -->
<TABLE height=64 cellSpacing=0 cellPadding=0 width=762 align=center border=0>
  <TBODY>
  <TR>
    <TD bgColor=#000000 colSpan=3 height=1></TD></TR>
  <TR>
    <TD width=1 bgColor=#000000><IMG height=1 src="" width=1></TD>
    <TD width=760 height=64>
      <TABLE height=64 cellSpacing=0 cellPadding=0 width="100%" align=center 
      border=0>
        <TBODY>
        <TR bgColor=#ffffff>
          <TD vAlign=center align=left width=180><A 
            href="http://www.javaresearch.org/index.jsp"><IMG 
            src="checkbox的jsp应用.files/logo.gif" border=0></A></TD>
          <TD vAlign=center align=middle width=468 height=60>
            <CENTER><IFRAME marginWidth=0 marginHeight=0 
            src="checkbox的jsp应用.files/topbanner.htm" frameBorder=0 width=468 
            scrolling=no height=60 
          valign="bottom">									</IFRAME></CENTER></TD>
          <TD vAlign=center align=right width=120><BR>2004年06月19日<BR><A 
            href="http://www.javaresearch.org/login.jsp"><FONT 
            color=black>登录/注册</FONT></A> </TD></TR></TBODY></TABLE>
      <TABLE height=21 cellSpacing=1 cellPadding=0 width="100%" bgColor=#995f14 
      border=0>
        <TBODY>
        <TR bgColor=#006699>
          <TD bgColor=#000000>
            <P align=right><FONT 
            color=#ffffff>[&nbsp;Java原创社区,当前在线:284,本次峰值:17075&nbsp;]</FONT>&nbsp;&nbsp;&nbsp;&nbsp; 
            <A href="http://www.javaresearch.org/index.jsp"><FONT 
            color=#ffffff>首页</FONT></A><FONT color=#ffffff> | </FONT><A 
            href="http://www.javaresearch.org/news/index.jsp"><FONT 
            color=#ffffff>Java新闻</FONT></A><FONT color=#ffffff> | </FONT><A 
            href="http://www.javaresearch.org/article/index.jsp"><FONT 
            color=#ffffff>研究文集</FONT></A><FONT color=#ffffff> | </FONT><A 
            href="http://www.javaresearch.org/forum/index.jsp"><FONT 
            color=#ffffff>专题论坛</FONT></A><FONT color=#ffffff> | </FONT><A 
            href="http://javaresearch.gro.clinux.org/index.html" 
            target=_blank><FONT color=#ffffff>开源软件</FONT></A><FONT 
            color=#ffffff> | </FONT><A 
            href="http://www.javaresearch.org/jrworks.jsp"><FONT 
            color=#ffffff>JR 作品</FONT></A><FONT color=#ffffff> | </FONT><A 
            href="http://www.javaresearch.org/dn.jsp"><FONT 
            color=#ffffff>精品下载</FONT></A><FONT color=#ffffff> | </FONT><A 
            href="http://www.javaresearch.org/aboutus.jsp"><FONT 
            color=#ffffff>关于我们</FONT></A><FONT color=#ffffff>&nbsp;&nbsp; 
            </FONT></P></TD></TR></TBODY></TABLE></TD>
    <TD width=1 bgColor=#000000><IMG height=1 src="" 
width=1></TD></TR></TBODY></TABLE><!-- start of news row -->
<TABLE height=180 cellSpacing=0 cellPadding=0 width=762 align=center border=0>
  <TBODY>
  <TR>
    <TD width=1 bgColor=#000000 height=180><IMG height=1 src="" width=1></TD><!--right column-->
    <TD vAlign=top width=760 bgColor=#ffffff height=180>
      <DIV align=center>
      <TABLE height=5 cellSpacing=0 cellPadding=0 width="100%" border=0><!--space-->
        <TBODY>
        <TR>
          <TD align=middle width="95%"><!-- content begin -->
            <TABLE cellSpacing=2 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD align=left width="50%"><B><A 
                  href="http://www.javaresearch.org/index.jsp">首页</A> &raquo; <A 
                  href="http://www.javaresearch.org/article/index.jsp">研究文集</A> 
                  &raquo; <A 
                  href="http://www.javaresearch.org/article/column.jsp?column=331">Java技巧集</A> 
                  </B></TD>
                <TD align=right width="50%"><A 
                  href="http://www.javaresearch.org/article/postarticle.jsp?column=331&amp;thread=6069&amp;reply=true"><IMG 
                  height=17 alt=评论此文章 src="checkbox的jsp应用.files/reply.gif" 
                  width=17 border=0></A>&nbsp;<A class=title 
                  title="Click to reply to this topic" 
                  href="http://www.javaresearch.org/article/postarticle.jsp?column=331&amp;thread=6069&amp;reply=true">发表评论</A> 
<!--&nbsp;&nbsp;<a href="/search.jsp?column=331"><img src="/images/search.gif" width="17" height="17" alt="搜索栏目" border="0"></a>&nbsp;<a href="/search.jsp?column=331" class="title" title="搜索栏目">搜索文章</a> --></TD></TR></TBODY></TABLE>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><!-- start of article title -->
                  <TABLE cellSpacing=0 cellPadding=0 width="100%" 
                  bgColor=#ffffff border=0>
                    <TBODY>
                    <TR>
                      <TD class=title1 align=middle width="100%" bgColor=#eeeeee 
                      colSpan=3 height=40><B>checkbox的使用</B></TD></TR>
                    <TR>
                      <TD align=middle width="100%" bgColor=#eeeeee><A 
                        href="http://www.javaresearch.org/profile.jsp?user=10258">lookbook</A> 
                        原创&nbsp; (参与分:267,专家分:750)&nbsp;&nbsp; 发表:2003-4-10 
                        下午8:29 &nbsp;&nbsp;更新:2003-4-12 下午9:56 
                        &nbsp;&nbsp;版本:1.0 
                    &nbsp;&nbsp;阅读:<B>5479</B>次</TD></TR></TBODY></TABLE></TD></TR><!-- end of article title -->
              <TR>
                <TD vAlign=top align=middle width="100%"><!--start of article content -->
                  <TABLE width="98%" border=0>
                    <TBODY>
                    <TR>
                      <TD class=text align=left width="100%"><BR>
                        <H3>checkbox的使用</H3><BR><BR>
                        <H5>1、checkbox定义</H5><BR>[pre]&nbsp;&nbsp;&nbsp;&nbsp;在jsp或者html页面中,经常会用到checkbox。比如这样定义:[/pre]<BR>&lt;input&nbsp;type=<FONT 
                        color=#000099>"checkbox"</FONT>&nbsp;name=<FONT 
                        color=#000099>"check_id"</FONT>&nbsp;size=<FONT 
                        color=#000099>"6"</FONT>&nbsp;value=<FONT 
                        color=#000099>"1"</FONT>&nbsp;&gt;<BR>&lt;input&nbsp;type=<FONT 
                        color=#000099>"checkbox"</FONT>&nbsp;name=<FONT 
                        color=#000099>"check_id"</FONT>&nbsp;size=<FONT 
                        color=#000099>"6"</FONT>&nbsp;value=<FONT 
                        color=#000099>"2"</FONT>&nbsp;&gt;<BR>...<BR>&lt;input&nbsp;type=<FONT 
                        color=#000099>"checkbox"</FONT>&nbsp;name=<FONT 
                        color=#000099>"check_id"</FONT>&nbsp;size=<FONT 
                        color=#000099>"6"</FONT>&nbsp;value=<FONT 
                        color=#000099>"9"</FONT>&nbsp;&gt;<BR><BR>[pre]提交后的我们可以直接从request对象中取得所有在页面中选择的checkbox列表。<BR>以上面的定义为例,如果在页面中选择了value为1、2、5的checkbox项,则可以<BR>这样获得选择项:[/pre]<BR>String[]&nbsp;checkIds&nbsp;=&nbsp;request.getParameterValues(<FONT 
                        color=#000099>"check_id"</FONT>);<BR><BR><BR>[pre]有可能一些初级的web程序员也会这样定义:[/pre]<BR>&lt;input&nbsp;type=<FONT 
                        color=#000099>"checkbox"</FONT>&nbsp;name=<FONT 
                        color=#000099>"check_id1"</FONT>&nbsp;size=<FONT 
                        color=#000099>"6"</FONT>&nbsp;value=<FONT 
                        color=#000099>"1"</FONT>&nbsp;&gt;<BR>&lt;input&nbsp;type=<FONT 
                        color=#000099>"checkbox"</FONT>&nbsp;name=<FONT 
                        color=#000099>"check_id2"</FONT>&nbsp;size=<FONT 
                        color=#000099>"6"</FONT>&nbsp;value=<FONT 
                        color=#000099>"2"</FONT>&nbsp;&gt;<BR>...<BR>&lt;input&nbsp;type=<FONT 
                        color=#000099>"checkbox"</FONT>&nbsp;name=<FONT 
                        color=#000099>"check_id9"</FONT>&nbsp;size=<FONT 
                        color=#000099>"6"</FONT>&nbsp;value=<FONT 
                        color=#000099>"9"</FONT>&nbsp;&gt;<BR><BR>[pre]然后,在后台取数据时这样获得选择项:[/pre]<BR>String&nbsp;checkId1&nbsp;=&nbsp;request.getParameterValues(<FONT 
                        color=#000099>"check_id1"</FONT>);<BR>String&nbsp;checkId2&nbsp;=&nbsp;request.getParameterValues(<FONT 
                        color=#000099>"check_id2"</FONT>);<BR>...<BR>String&nbsp;checkId9&nbsp;=&nbsp;request.getParameterValues(<FONT 
                        color=#000099>"check_id9"</FONT>);<BR><BR>[pre]这样也可以解决问题,但是比较起来,后者用的是死办法,而且维护起来<BR>非常不便,在此不推荐。[/pre]<BR><BR>
                        <H5>2、checkbox的校验</H5><BR>[pre]&nbsp;&nbsp;&nbsp;&nbsp;经常有这样的情况,在页面中,有一批动态的checkbox列表项和<BR>一个提交的按钮,业务逻辑需要至少选择一个列表项数据之后,才可以真正<BR>提交。可以用一个javascript解决:<BR>假设页面定义和相关代码如下:[/pre]<BR><BR>&lt;script&nbsp;language=javascript&gt;<BR>function&nbsp;okClick(){<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#006600><I>//获得选择的check列表对象</FONT></I><BR>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;checkIdObj&nbsp;=&nbsp;eval(<FONT 
                        color=#000099>"form1.check_id"</FONT>);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#006600><I>//判断对象是否为空</FONT></I><BR>&nbsp;&nbsp;&nbsp;&nbsp;<B>if</B>&nbsp;(checkIdObj&nbsp;==&nbsp;null)&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>return</B>;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<B>for</B>&nbsp;(i=0;&nbsp;i&lt;checkIdObj.length;&nbsp;i++)&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>if</B>(checkIdObj[i].checked){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;form1.submit();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>return</B>;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR>&lt;/script&gt;<BR>&lt;form&nbsp;name=<FONT 
                        color=#000099>"form1"</FONT>&nbsp;method=<FONT 
                        color=#000099>"post"</FONT>&nbsp;action=<FONT 
                        color=#000099>".."</FONT>&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--&nbsp;下面的checkbox列表是动态的,有可能是从数据库中读取出来,所以有可能列表中只有一个数据--&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type=<FONT 
                        color=#000099>"checkbox"</FONT>&nbsp;name=<FONT 
                        color=#000099>"check_id"</FONT>&nbsp;size=<FONT 
                        color=#000099>"6"</FONT>&nbsp;value=<FONT 
                        color=#000099>"1"</FONT>&nbsp;&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type=<FONT 
                        color=#000099>"checkbox"</FONT>&nbsp;name=<FONT 
                        color=#000099>"check_id"</FONT>&nbsp;size=<FONT 
                        color=#000099>"6"</FONT>&nbsp;value=<FONT 
                        color=#000099>"2"</FONT>&nbsp;&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type=<FONT 
                        color=#000099>"button"</FONT>&nbsp;name=<FONT 
                        color=#000099>"ok"</FONT>&nbsp;onclick=<FONT 
                        color=#000099>"okClick()"</FONT>&nbsp;value=<FONT 
                        color=#000099>"确定"</FONT>&gt;<BR>&lt;/form&gt;<BR><BR>[pre]上面的定义中,如果checkbox列表中有多余一个数据时,可以工作正常,但是如果只<BR>有一个的话(因为checkbox列表是动态的),那么永远都不会提交(不信你可以试一试)。<BR>&nbsp;&nbsp;&nbsp;&nbsp;经过研究,发现原来是html语言的定义有问题。如果checkbox的列表项只有一个<BR>的话,那么checkIdOjb.length是不存在的。<BR>所以,可以将以上的javascript改一下:[/pre]<BR>&lt;script&nbsp;language=javascript&gt;<BR>function&nbsp;okClick(){<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#006600><I>//获得选择的check列表对象</FONT></I><BR>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;checkIdObj&nbsp;=&nbsp;eval(<FONT 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -