zl程序教程

您现在的位置是:首页 >  前端

当前栏目

jQuery中Form相关知识汇总

jQuery 知识 相关 汇总 Form
2023-06-13 09:15:39 时间

form中的单行文本获取和失去焦点

复制代码代码如下:


<!DOCTYPEhtml>
<html>
<headlang="en">
   <metacharset="UTF-8">
   <scripttype="text/javascript"src="../../js/jquery-2.1.3.js"></script>
   <title></title>
   <styletype="text/css">
       input:focus,textarea:focus{
           border:1pxsolid#f00;
           background:#fcc;
       }
       .focus{
           border:1pxsolid#f00;
           background:#fcc;
       }
   </style>
</head>
<body>
<formaction="#"method="post"id="regForm">
   <fieldset>
       <legend>个人基本信息</legend>
       <div>
           <labelfor="username">名称:</label>
           <inputid="username"type="text">
       </div>
       <div>
           <labelfor="pass">密码:</label>
           <inputid="pass"type="password">
       </div>
       <div>
           <labelfor="msg">详细信息:</label>
           <textareaid="msg"></textarea>
       </div>
   </fieldset>
</form>
</body>
<scripttype="text/javascript">
   /**
    *1.单行文本框---得到焦点和失去焦点
    **/
   $(function(){
       $(":input").focus(function(){
           $(this).addClass("focus");
       }).blur(function(){
           $(this).removeClass("focus");
       })
   })
</script>
</html>

更改多行文本的高度

复制代码代码如下:


<!DOCTYPEhtml>
<html>
<headlang="en">
   <metacharset="UTF-8">
   <scripttype="text/javascript"src="../../js/jquery-2.1.3.js"></script>
   <title></title>
   <styletype="text/css">
       *{margin:0;padding:0;font:normal12px/17pxArial;}
       .msg{width:300px;margin:100px;}
       .msg_caption{width:100%;overflow:hidden;margin-bottom:1px;}
       .msg_captionspan{display:block;float:left;margin:02px;padding:4px10px;background:#898989;cursor:pointer;color:white;}
       .msgtextarea{width:300px;height:80px;height:100px;border:1pxsolid#000;}
   </style>
</head>
<body>
<form>
   <divclass="msg">
       <divclass="msg_caption">
           <spanclass="bigger">放大</span>
           <spanclass="smaller">缩小</span>
       </div>
       <textareaid="comment"rows="8"cols="20">多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
       </textarea>
   </div>
</form>
</body>
<scripttype="text/javascript">
   /**
    *多行文本框的高度调整
    **/
   $(function(){
       var$comment=$("#comment");
       $(".bigger").click(function(){
           if(!$comment.is(":animated")){
               if($comment.height()<500){
                   //$comment.height($comment.height()+50);//版本1
                   $comment.animate({height:"+=50"},400);
               }
           }
       });
       $(".smaller").click(function(){
           if(!$comment.is(":animated")){
               if($comment.height()>50){
                   //$comment.height($comment.height()-50);
                   $comment.animate({height:"-=50"},400);
               }
           }
       });
   });
</script>
</html>

更改多行文本的滚动条高度

复制代码代码如下:
<!DOCTYPEhtml>
<html>
<headlang="en">
   <metacharset="UTF-8">
   <scripttype="text/javascript"src="../../js/jquery-2.1.3.js"></script>
   <title></title>
   <styletype="text/css">
       *{margin:0;padding:0;font:normal12px/17pxArial;}
       .msg{width:300px;margin:100px;}
       .msg_caption{width:100%;overflow:hidden;margin-bottom:1px;}
       .msg_captionspan{display:block;float:left;margin:02px;padding:4px10px;background:#898989;cursor:pointer;color:white;}
       .msgtextarea{width:300px;height:80px;height:100px;border:1pxsolid#000;}
   </style>
</head>
<body>
<form>
   <divclass="msg">
       <divclass="msg_caption">
           <spanclass="up">向上</span>
           <spanclass="down">向下</span>
       </div>
       <textareaid="comment"rows="8"cols="20">多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
           多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。多行文本框高度变化。
       </textarea>
   </div>
</form>
</body>
<scripttype="text/javascript">
   /**
    *多行文本框的滚动条高度调整
    **/
   $(function(){
       var$comment=$("#comment");
       $(".up").click(function(){
           if(!$comment.is(":animated")){
               if($comment.height()<500){
                   $comment.animate({scrollTop:"+=50"},400);
               }
           }
       });
       $(".down").click(function(){
           if(!$comment.is(":animated")){
               if($comment.height()>50){
                   $comment.animate({scrollTop:"-=50"},400);
               }
           }
       });
   });
</script>
</html>

复选框应用

复制代码代码如下:
<!DOCTYPEhtml>
<html>
<headlang="en">
   <metacharset="UTF-8">
   <scripttype="text/javascript"src="../../js/jquery-2.1.3.js"></script>
   <title></title>
   <styletype="text/css">
       input:focus,textarea:focus{
           border:1pxsolid#f00;
           background:#fcc;
       }
       .focus{
           border:1pxsolid#f00;
           background:#fcc;
       }
   </style>
</head>
<body>
<form>
   你爱好的运动是?<br/>
   <inputtype="checkbox"name="items"value="足球"/>足球
   <inputtype="checkbox"name="items"value="篮球"/>篮球
   <inputtype="checkbox"name="items"value="羽毛球"/>羽毛球
   <inputtype="checkbox"name="items"value="乒乓球"/>乒乓球
   <inputtype="button"id="checkedAll"value="全 选"/>
   <inputtype="button"id="checkedNo"value="全不选"/>
   <inputtype="button"id="checkedRev"value="反 选"/>
   <inputtype="button"id="send"value="提交"/>
</form>
</body>
<scripttype="text/javascript">
   /**
    *复选框应用
    **/
   $(function(){
       $("#checkedAll").click(function(){
           $("[name=items]:checkbox").attr("checked",true);
       });
       $("#checkedNo").click(function(){
           $("[name=items]:checkbox").attr("checked",false);
       });
       $("#checkedRev").click(function(){
           $("[name=items]:checkbox").each(function(){
               this.checked=!this.checked;
           });
       });
       $("#send").click(function(){
           varstr="你选中的是:\r\n";
           $("[name=items]:checkbox:checked").each(function(){
               str+=$(this).val()+"\r\n";
           });
           alert(str);
       });
   })
</script>
</html>