`

JavaScript实现checkbox全选,当只有一个时不起作用的情况也已经解决

阅读更多
转载:http://hi.baidu.com/javajavajava/blog
<script language="JavaScript">
   function check(field) {
    var t = document.noticeForm.selectAll;
    if(!document.noticeForm.notice.length){ 
         if(t.checked){ 
             document.noticeForm.notice.checked=true; 
         } 
         else{ 
             document.noticeForm.notice.checked=false;   
         } 
         }
   
    if (t.checked) {
     for (i = 0; i <field.length; i++) {
      field[i].checked = true;
     }
    }
    else {
     for (i = 0; i <field.length; i++) {
      field[i].checked = false; 
     }
    }
   }
   function isSelect(confirmString){
    field = document.noticeForm.notice;
    var count = 0;
    if(!document.noticeForm.notice.length){ 
          if(document.noticeForm.notice.checked){ 
              count++; 
          } 
         }
    for (i = 0; i <field.length; i++) {
     if(field[i].checked == true){
      ++count;
     }
    }
    alert(count);
    if(count>0){
     return confirm(confirmString);
    } 
    else {
     alert('请至少选择一个!');
     return false;
    }
   }
  
</script>

<form action="${action}" name="noticeForm" onsubmit="return isSelect('${confirmString}');">
    <table border="1" width="95%" bordercolor="#8AB78A">
    <tr>
       <td align="center"><input type="checkbox" name="" onClick="check(this.form.notice)"/></td>
       <td align="center"><strong>状态</strong></td>
       <td align="center"><strong>标题</strong></td>
       <td align="center"><strong>作者</strong></td>
       <td align="center"><strong>发给谁?</strong></td>
       <td align="center"><strong>人气/回复</strong></td>
       <td align="center"><strong>发布时间</strong></td>
       <td align="center"><strong>最后修改</strong></td>
       <td align="center"><strong>审核</strong></td>
         </tr> 
    <c:forEach var="notice" items="${pageForm.list}" begin="${(pageForm.currentPage-1)*(pageForm.pageSize)}" end="${(pageForm.currentPage-1)*(pageForm.pageSize)+pageForm.pageSize-1}">
    <tr align="center">
       <td><input type="checkbox" name="notice" value="${notice.id}"/></td>
       <td><c:out value="${notice.statusString}"></c:out></td>
       <td>
        <a href="servlet/NoticeDetail?id=${notice.id}" title="${notice.title}">
        <c:out value="${notice.title}"></c:out>
        </a>
        <fmt:formatDate var="writeTime" value='${notice.writeTime}' type='both' pattern="yyyy-MM-dd"/>
        <jsp:useBean id="now" class="java.util.Date" /> 
     <fmt:formatDate var="today" value="${now}" type="both" pattern="yyyy-MM-dd" /> 
        <c:if test="${writeTime==today}">
         <img alt="新公告" src="images/new.gif">
        </c:if>
       </td>
       <td><c:out value="${notice.authorId}"></c:out></td>
       <c:set var="author" value="${notice.authorId}"></c:set>
       <td><c:out value="${notice.typeString}"></c:out>
       <c:out value="(${notice.recipients})"></c:out></td>
       <td><c:out value="${notice.readCount}"></c:out>/<c:out value="${notice.replyCount}"></c:out></td>
       <td>
       <fmt:formatDate value='${notice.writeTime}' type='both' pattern="yyyy-MM-dd HH:mm:ss"/>
       </td>
       <td>
        <fmt:formatDate value='${notice.lastModified}' type='both' pattern="yyyy-MM-dd HH:mm:ss"/>
       </td>
       <td>
        <c:out value="${notice.isAuditString}"></c:out>
       </td>
</tr>
              </c:forEach>
    </table>
    
    <input type="checkbox" name="selectAll" value="全选" onClick="check(this.form.notice)">全选
      <input type="image" src="images/delete.gif" style="cursor: hand;" alt="删除" >删除选定内容


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics