JQuery的表单的添加、删除

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>

</head>
<body>
   姓名:<input type="text" id="np">  年龄:<input type="number" id="pu"> 性别:<input type="text" id="ut" >   <input type="button" value="添加" id="in" >
   <table>
       <tr>
           <th><input type='checkbox' id="ip">全选</th>
           <th> 姓名</th>
           <th> 年龄</th>
           <th> 性别</th>
           <th> 操作</th>
       </tr>

   </table>
  <script>
      var n=0;
      $("#in").click(function () {
            //获取输入的姓名
          var xm=   $("#np").val();
          var nl=   $("#pu").val();
          var xb=   $("#ut").val();
          //  alert(  $("#np").val())
          //创建节点
          $("table").append("<tr>"+"<td>"+"<input type='checkbox'>"+"</td>"+"<td>"+xm+"</td>"+"<td>"+nl+"</td>"+"<td>"+xb+"</td>"+"<td>"+"<span>删除</span>"+"</td>"+"</tr>");

          $("table tr td span").click(function () {
               //判断当前这个复选框被勾选了才能被删除
              if($(this).parents("tr").find("input").prop("checked")==true){

                  $(this).parents("tr").remove();

              }

          });

      });

      //给"全选"一个点击事件,如果全选就把添加的input都设为true
      $("#ip").click(function () {
          if($("#ip").prop("checked")==true){
              $("tr td input").prop("checked",true);
          }else{
              $("tr td input").prop("checked",false);
          }
      });


      //把添加的input设置点击事件,在进行判断是否被勾选,计数器勾选一个就累计一个,然后判断input的长度和计数器是否相等,相等的话就把“全选的input勾选”
      $("table").on("click","input",function () {
              alert(0);
          if($(this).prop("checked")==true){
              n++;
          }else{
              n--;
          }
          if(n==$("tr td input").length){
              $("#ip").prop("checked",true);
          }else{
              $("#ip").prop("checked",false);
          }
      });


  </script>

</body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值