jQuery整理筆記七----幾個經典表單應用

來源:互聯網
上載者:User

1、文字框獲得(失去)焦點

當文字框獲得輸入焦點時,將該文字框高亮顯示,算不得一個應用,只是一個小技巧,可以提高使用者體驗。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><style type="text/css">body{font:normal 12px/17px Arial;}div{    padding:2px;} input, textarea {  width: 12em;  border: 1px solid #888;}.focus {  border: 1px solid #f00; background: #fcc;} </style><!--   引入jQuery --><script src="jquery-2.1.0.min.js" type="text/javascript"></script><script type="text/javascript">    $(function(){$(":input").focus(function(){  $(this).addClass("focus");  if($(this).val() ==this.defaultValue){                    $(this).val("");             } }).blur(function(){ $(this).removeClass("focus"); if ($(this).val() == '') {                $(this).val(this.defaultValue);             }});    })    </script></head><body><form action="" method="post" id="regForm"><fieldset><legend>個人基本資料</legend><div><label  for="username">名稱:</label><input id="username" type="text" value="名稱" /></div>                <div><label for="pass">密碼:</label><input id="pass" type="password" value="密碼" /></div>                <div><label for="msg">詳細資料:</label><textarea id="msg" rows="2" cols="20">詳細資料</textarea></div></fieldset></form></body></html>

:


2、Elastic彈性文本域

Elastic是一款功能專一的表單外掛程式,他可以控制頁面內表單域(<textarea>)標籤高度自動調整,以適應包含的文本。應用這個外掛程式的時候頁面需要引入jquery.elastic.source.js。

外掛程式:點擊進入下載頁面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <script  src="jquery-2.1.0.min.js" type="text/javascript"></script>  <script src="jquery.elastic.source.js" type="text/javascript" ></script>  <script type="text/javascript">//頁面載入方法$(function(){$("textarea").elastic();//應用彈性文字框})  </script> </head> <body><textarea name="" rows="2" cols="43">沁園春·雪北國風光,千裡冰封,萬裡雪飄。望長城內外,惟餘莽莽;大河上下,頓失滔滔。山舞銀蛇,原驅蠟象,欲與天公試比高。須晴日,看紅裝素裹,分外妖嬈。江山如此多嬌,引無數英雄競折腰。惜秦皇漢武,略輸文采;唐宗宋祖,稍遜風騷。一代天驕,成吉思汗,只識彎弓射大雕。俱往矣,數風流人物,還看今朝。</textarea> </body></html>

:


我們最初設定的<textarea>標籤的rows屬性值為2 ,隨著常值內容的增多高度會自動增加,當然了,隨著內容的減少也可以高度降低的。

3、Autotab自動Tab文字框

Autotab也是一款功能專一的表單外掛程式,它提供了自動跳格的功能,當使用者輸入的字元數一旦超過已定義的最大長度,則會根據事先設定的目標自動跳轉到相應元素上,省卻了

使用者按【Tab】鍵的麻煩。最典型的應用就是輸入IP地址、軟體啟用碼等地方了,我們做的web項目中也有很多地方可以用到這外掛程式,對於提高使用者體驗還是很有協助的。

使用時需要引入jquery.autotab.js,:點擊進入下載頁面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <script  src="jquery-2.1.0.min.js" type="text/javascript"></script>  <script  src="jquery.autotab.js" type="text/javascript"></script>  <script type="text/javascript">//頁面載入方法$(function(){$('#autotab').submit(function(){return false;})$('#autotab :input').autotab_magic();//為頁面文字框綁定autotab外掛程式})  </script> </head> <body><h1>jQuery整理筆記七</h1><h2>Autotab自動Tab文字框</h2><form method="post" action=""  id="autotab"><label>請輸入驗證碼:<input type="text" name="num1" id="num1" maxlength="3"  size="3"><input type="text" name="num2" id="num2" maxlength="3"  size="3"><input type="text" name="num3" id="num3" maxlength="3"  size="3"><input type="text" name="num4" id="num4" maxlength="3"  size="3"><input type="text" name="num5" id="num5" maxlength="3"  size="3"><input type="text" name="num6" id="num6" maxlength="3"  size="3"></form> </body></html>

除了可以限定輸入長度外,還可以通過autotab_filter()方法限定輸入的字元類型,這個方法還能過濾大寫、小寫、空格、字母等,具體的用到了現查吧。

如果將上面的js改成:

$(function(){$('#autotab').submit(function(){return false;});$('#autotab :input').autotab_magic().autotab_filter('numeric');})
就是只能輸入數字了。

4、passwordStrength密碼強度指標

passwordStrength外掛程式能夠根據使用者輸入的密碼,以圖形化方式顯示密碼的強度。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>passwordStrength</title><link href="css/style.css" rel="stylesheet" type="text/css" media="all" /><script type="text/javascript" src="jquery-2.1.0.min.js"></script><script type="text/javascript" src="passwordStrength.js"></script><script type="text/javascript">$(function(){$('input[name="password"]').passwordStrength();})</script><style type="text/css">.is0{background:url(images/progressImg1.png) no-repeat 0 0;width:138px;height:7px;margin:10px 0 0 104px;}.is10{background-position:0 -7px;}.is20{background-position:0 -14px;}.is30{background-position:0 -21px;}.is40{background-position:0 -28px;}.is50{background-position:0 -35px;}.is60{background-position:0 -42px;}.is70{background-position:0 -49px;}.is80{background-position:0 -56px;}.is90{background-position:0 -63px;}.is100{background-position:0 -70px;} #autotab input { width:138px; }</style></head><body><h1>jQuery整理筆記七</h1><h2>表單開發(Forms)</h2><hr /><h3>passwordStrength密碼強度指標</h3><form action="" method="post" id="autotab" class="p1">    <label>請輸入密碼:    <input type="password" name="password" />        <div id="passwordStrengthDiv" class="is0"></div>    </label></form></body></html>
上例用到一個圖片:

執行:

5、formToWizard表單填充嚮導

這個是什麼意思呢?其實我們實際見的也很多,有很多網站填寫註冊資訊的時候是分步進行的,比方說,先填寫個人資訊,然後再填寫工作資訊...然後一起提交。這就避免了龐

大的資訊量都在一個頁面上進行填寫。

formToWizard就是解決這個問題的一個小外掛程式,外掛程式:點擊進入下載頁面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <script  src="jquery-2.1.0.min.js" type="text/javascript"></script>  <script type="text/javascript" src="formToWizard.js"></script>  <script type="text/javascript">//頁面載入方法$(function(){$("#form1").formToWizard({ submitButton: 'SaveAccount' })})  </script>  <style type="text/css">#wrap { margin:1em 4em; font-size:12px; padding:1em 1em; border:solid 1px #fff; }fieldset { border:none; width:320px; }legend { font-size:18px; margin:0px; padding:10px 0px; color:#b0232a; font-weight:bold; }label { display:block; margin:15px 0 5px; }input[type=text], input[type=password] { width:300px; padding:5px; border:solid 1px #000; }.prev, .next { background-color:#b0232a; padding:5px 10px; color:#fff; text-decoration:none; }.prev:hover, .next:hover { background-color:#000; text-decoration:none; }.prev { float:left; }.next { float:right; }#steps { list-style:none; width:100%; overflow:hidden; margin:0px; padding:0px; }#steps li { font-size:24px; float:left; padding:10px; color:#b0b1b3; }#steps li span { font-size:11px; display:block; }#steps li.current { color:#000; }#makeWizard { background-color:#b0232a; color:#fff; padding:5px 10px; text-decoration:none; font-size:18px; }#makeWizard:hover { background-color:#000; }  </style> </head> <body> <div id="wrap">      <form id="form1" action="">        <fieldset>            <legend>登入資訊</legend>            <label for="Name">暱稱</label>            <input id="Name" type="text" />            <label for="Email">Email</label>            <input id="Email" type="text" />            <label for="Password">密碼</label>            <input id="Password" type="password" />        </fieldset>        <fieldset>            <legend>公司資訊</legend>            <label for="CompanyName">公司名稱</label>            <input id="CompanyName" type="text" />            <label for="Website">公司網址</label>            <input id="Website" type="text" />            <label for="CompanyEmail">公司郵箱</label>            <input id="CompanyEmail" type="text" />        </fieldset>        <fieldset>            <legend>個人資訊</legend>            <label for="NameOnCard">真實姓名</label>            <input id="NameOnCard" type="text" />            <label for="CardNumber">社會安全號碼</label>            <input id="CardNumber" type="text" />            <label for="CreditcardMonth">發卡日期</label>            <select id="CreditcardMonth">                <option value="1">1</option>                <option value="2">2</option>                <option value="3">3</option>                <option value="4">4</option>                <option value="5">5</option>                <option value="6">6</option>                <option value="7">7</option>                <option value="8">8</option>                <option value="9">9</option>                <option value="10">10</option>                <option value="11">11</option>                <option value="12">12</option>            </select>            <select id="CreditcardYear">                <option value="2009">2009</option>                <option value="2010">2010</option>                <option value="2011">2011</option>            </select>            <label for="Address1">地址1</label>            <input id="Address1" type="text" />            <label for="Address2">地址2</label>            <input id="Address2" type="text" />            <label for="City">城市</label>            <input id="City" type="text" />            <label for="Country">國家</label>            <select id="Country">                <option value="CA">Canada</option>                <option value="US">United States of America</option>                <option value="GB">United Kingdom (Great Britain)</option>                <option value="AU">Australia</option>                <option value="JP">Japan</option>            </select>        </fieldset>        <div>            <input id="SaveAccount" type="button" value="提交表單" />        </div></div>    </form> </body></html>

:

6、checkbox複選框(全選反選等操作)

以前經常用,不說了。

7、下拉框的應用

這回先看個圖:

大家肯定都見過類似效果的網頁,怎麼實現的呢,代碼很簡單:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><style type="text/css">* { margin:0; padding:0; }div.centent {   float:left;   text-align: center;   margin: 10px;}span { display:block; margin:2px 2px;padding:4px 10px; background:#898989;cursor:pointer;font-size:12px;color:white;}</style><!--   引入jQuery --><script src="jquery-2.1.0.min.js" type="text/javascript"></script><script type="text/javascript">$(function(){//移到右邊$('#add').click(function() {//擷取選中的選項,刪除並追加給對方$('#select1 option:selected').appendTo('#select2');});//移到左邊$('#remove').click(function() {$('#select2 option:selected').appendTo('#select1');});//全部移到右邊$('#add_all').click(function() {//擷取全部的選項,刪除並追加給對方$('#select1 option').appendTo('#select2');});//全部移到左邊$('#remove_all').click(function() {$('#select2 option').appendTo('#select1');});//雙擊選項$('#select1').dblclick(function(){ //綁定雙擊事件//擷取全部的選項,刪除並追加給對方$("option:selected",this).appendTo('#select2'); //追加給對方});//雙擊選項$('#select2').dblclick(function(){   $("option:selected",this).appendTo('#select1');});});</script></head><body><div class="centent"><select multiple="multiple" id="select1" style="width:100px;height:160px;"><option value="1">曹操</option><option value="2">曹昂</option><option value="3">曹丕</option><option value="4">曹彰</option><option value="5">曹植</option><option value="6">曹熊</option><option value="7">曹仁</option><option value="8">曹洪</option><option value="9">曹休</option><option value="10">曹真</option><option value="11">曹爽</option></select><div><span id="add" >選中添加到右邊>></span><span id="add_all" >全部添加到右邊>></span></div></div><div class="centent"><select multiple="multiple" id="select2" style="width: 100px;height:160px;"><option value="12">曹芳</option></select><div><span id="remove"><<選中刪除到左邊</span><span id="remove_all"><<全部刪除到左邊</span></div></div></body></html>

代碼實現的功能:

1)、將選中的選項添加給對方

2)、將全部選項添加給對方

3)、雙擊某個選項將其添加給對方





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.