checkbox 应用实例

本文介绍了一个使用HTML和JavaScript实现的复选框应用实例。该实例包含一个全选按钮及多个带链接的复选框,当选择不同的选项时,会在文本框中显示已选项目的汇总信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用实例:

<%@ page contentType="text/html; charset=utf-8" language="java"
buffer="18kb"%>
<html>
<head>
   <title>CheckBox_Application</title>
</head>
<body>
   <table id="table1">
    <tr>
     <th><input type='checkbox' onclick='javascript: checkAll(this)'>全选</th>
    </tr>
    <tr>
      <td><input type='checkbox' value="汽车" title="car" onclick="callCheck(this)">
            <a onclick="linkDetail('汽车')" style="cursor:hand">汽车</a></td>
    </tr>
    <tr>
      <td><input type='checkbox' value="房子" title="house" onclick="callCheck(this)">
            <a onclick="linkDetail('房子')" style="cursor:hand">房子</a></td>
    </tr>
    <tr>
       <td><input type='checkbox' value="朋友" title="friends"   onclick="callCheck(this)">
            <a onclick="linkDetail('朋友')" style="cursor:hand">朋友</a></td>
    </tr>
    <tr>
       <td><input type='checkbox' value="亲人" title="famaliy"   onclick="callCheck(this)">
             <a onclick="linkDetail('亲人')" style="cursor:hand">亲人</a></td>
    </tr>
   </table>
   <br>
   <input type="text" name="context" size="100">
   <input type="hidden" name="contextTemp" size="100">
</body>
<script type="text/javascript" language="javascript">       
   function checkAll(o) {
    var mytable = document.getElementById("table1").getElementsByTagName("input");
    if(o.checked){//在进行全选时,把以前单独选中的数据清除,避免重复
     document.getElementById("contextTemp").value = "";
     document.getElementById("context").value = "";   
    }
    for(var i=1,ci;ci=mytable[i++];) {         
     ci.checked = o.checked;
     callCheck(ci);
    }        
   }
  
   function callCheck(obj){
   var initVal = "您选择了:";
   var contectTemp = document.getElementById("contextTemp").value;
   var opVal = obj.value;
   if(obj.checked){//选中的数据项
      if(contectTemp==""||contectTemp==null){
       contectTemp = opVal;
      }else{
       contectTemp+=";"+ opVal;
      }
   }else{//取消选中项
     contectTemp = contectTemp.replaceAll(opVal+";","");
      contectTemp = contectTemp.replaceAll(";"+opVal,"");
      contectTemp = contectTemp.replaceAll(opVal,"");
    }
   if(contectTemp!=""&&contectTemp!=null){//当有选中项时,根据规则把选中内容写到内容框中
     document.getElementById("contextTemp").value = contectTemp;
      document.getElementById("context").value = initVal + contectTemp;
   }else{//当所有项都取消,同时初始化内容框
      document.getElementById("contextTemp").value = "";
      document.getElementById("context").value = "";
   }
   }
   String.prototype.replaceAll = function(s1,s2){
   return this.replace(new RegExp(s1,"gm"),s2);
   }   
   function linkDetail(name){
    alert("弹出有关:"+name+"的信息!");
   }
</script>
</html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值