jquery ui 插件实例 (4)

来源:互联网 发布:手机文档软件下载 编辑:程序博客网 时间:2024/04/30 05:36

4、模 拟提 交 表 单 效 果

js效果

[javascript] view plaincopyprint?
  1. <link rel="stylesheet" href="../css/ui-lightness/jquery-ui-1.8.18.custom.css" />  
  2. <script language="javascript" type="text/javascript" src="../js/jquery-1.7.1.min.js">  
  3. </script>  
  4. <script language="javascript" type="text/javascript" src="../js/jquery-ui-1.8.18.custom.min.js"></script>  
  5. <script language="javascript" type="text/javascript">  
  6.   
  7.   
  8. $(function(){  
  9.     //----------------------------------------------------------------------------------  
  10. var tips = $( ".validateTips" );  
  11.   
  12.         //错误提示 css 样式修改  
  13.             function updateTips( t ) {                           
  14.             tips.text( t ).addClass( "ui-state-highlight" );  
  15.             setTimeout(function() {  
  16.                 tips.removeClass( "ui-state-highlight", 1500 );  
  17.             }, 500 );  
  18.         }  
  19.           
  20.           
  21.          //判断输入值  长度   是否正确  
  22.                 function checkLength( o, n, min, max ) {           
  23.             if ( o.val().length > max || o.val().length < min ) {  
  24.                 o.addClass( "ui-state-error" );  
  25.                 updateTips( "Length of " + n + " must be between " +  
  26.                     min + " and " + max + "." );  
  27.                 return false;  
  28.             } else {  
  29.                 return true;  
  30.             }}  
  31.               
  32.               
  33.             //判断输入值  格式  是否正确  
  34.                 function checkRegexp( o, regexp, n ) {  
  35.             if ( !( regexp.test( o.val() ) ) ) {  
  36.                 o.addClass( "ui-state-error" );  
  37.                 updateTips( n );  
  38.                 return false;  
  39.             } else {  
  40.                 return true;  
  41.             }  
  42.         }     
  43.               
  44. //----------------------------------------------------------------------------------------            
  45.   
  46. $("#dialog").dialog({  
  47.     autoOpen:false,  
  48.     show:"blind",  
  49.     hide:"explode",  
  50.     modal:true,  
  51.     buttons:{                 
  52.     "确定":function(){  
  53. //-----------------------------------------正则判断输入值的格式是否符合标准-----------------------------------------------------------------          
  54.             var bValid = true;  
  55. var name=$("#name");  
  56. var email=$("#email");  
  57. var password=$("#password");  
  58.                     bValid = bValid && checkLength( name, "username", 3, 16 );  
  59.                     bValid = bValid && checkLength( email, "email", 6, 80 );  
  60.                     bValid = bValid && checkLength( password, "password", 5, 16 );  
  61.   
  62.                     bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );  
  63.                     // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/  
  64.                     bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );  
  65.                     bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );  
  66.   
  67.           
  68.           
  69. //----------------------------------------------------------------------------------------------------------          
  70.         if(bValid){  
  71.         $("#users-contain tbody").append("<tr><td>"+$("#name").val()+"</td><td>"+$("#email").val()+"</td><td>"+$("#password").val()+"</td></tr>");  
  72.         $(this).dialog("close");} //关闭对话框  
  73.         },  
  74.     "取消":function(){  
  75.         $(this).dialog("close");   
  76.         }  
  77.     },  
  78.         closeOnEscape:false,  
  79.         title:"",  
  80.         position:"top",  
  81.       
  82.   
  83. })  
  84. $("#create-user").click(function(){  
  85.     $("#dialog").dialog("open");  
  86.       
  87.     });  
  88.     })  
  89.   
  90.   
  91. </script>  


html格式

[html] view plaincopyprint?
  1. <div class="demo">  
  2.   
  3. <div id="dialog" title="Create new user" style="width:200px; height:200px">  
  4.     <p class="validateTips">All form fields are required.</p>  
  5.   
  6.     <form>  
  7.     <fieldset>  
  8.         <label for="name">Name</label><br />  
  9.   
  10.         <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" /><br />  
  11.   
  12.         <label for="email">Email</label><br />  
  13.   
  14.         <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" /><br />  
  15.   
  16.         <label for="password">Password</label><br />  
  17.   
  18.         <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />  
  19.     </fieldset>  
  20.     </form>  
  21. </div>  
  22.   
  23.   
  24. <div id="users-contain" class="ui-widget">  
  25.     <h1>模 拟 表 单:</h1>  
  26.     <table id="users" class="ui-widget ui-widget-content">  
  27.         <thead>  
  28.             <tr class="ui-widget-header ">  
  29.                 <th>Name</th>  
  30.                 <th>Email</th>  
  31.                 <th>Password</th>  
  32.             </tr>  
  33.         </thead>  
  34.         <tbody>  
  35.             <tr>  
  36.                 <td>John Doe</td>  
  37.                 <td>john.doe@example.com</td>  
  38.                 <td>johndoe1</td>  
  39.             </tr>  
  40.         </tbody>  
  41.     </table>  
  42. </div>  
  43. <br />  
  44. <br />  
  45. <br />  
  46. <br />  
  47. <br />  
  48.   
  49. <input type="button" id="create-user" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="Create new user" />  
  50.   
  51. </div>  


 

 

实际效果图例:

原创粉丝点击