一个比较实用的jQuery例子

 

1.要使用jQuery框架,要引入如下:
    <script src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script>


2. val函数,它表示名为userName的input标签的控件的值    
 var userName = $( 'input[name="userName"]' ).val(); 


 3.append函数,css函数 
$("#msg").append("<p>Passwords don't match. </p>").css("color", "red");


4.attr函数 
$(this).attr("disabled", "disabled");5.each函数 $.each(states, function(index, value){            
       $("[name='stateCombo']").append("<option value='"                                                                     
                                        + index   + "'>"+ val  + "</option>");


6.extend函数        
   var CA_Cities = ["San Francisco", "Los Angeles", "Mountain View"];          
   var cities = [];          
   if(selectedState == 0){     
          cities = $.extend([], CA_Cities);            
   }




 7.清空函数        
    $("[name='cityCombo']").empty();


8.不用多说,附上代码
<!DOCTYPE html>
<html>
  <head>
    <title>User Registration and Validation</title>
    <script src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script>
      $(function(){
        $( 'input[name="validate"]' ).click(function(){
           // clear message div
           $( "#msg" ).html( "" );
 
           // get values for all input boxes
           var userName = $( 'input[name="userName"]' ).val();  
           var email = $( 'input[name="email"]' ).val();  
           var pass1 = $( 'input[name="password"]' ).val();  
           var pass2 = $( 'input[name="chkPassword"]' ).val();  
 
           // no empty values permitted
           var hasValue = userName && email && pass1 && pass2;
           if( !hasValue ){
               $( "#msg" )
               .append( "All Fields are required." )
               .css( "color","red" );
               return false;
           }
           // check that passwords match
           var passwordMatch = false;
           if( pass1 === pass2 ) {
               passwordMatch = true;
           }
 
           if( !passwordMatch ){
               $("#msg").append("<p>Passwords don't match. </p>").css("color", "red");
               return false;
           } 
        });
       
        $( "input[name='addLocation']" ).click(function(){
            $( "body" ).append( "<select name='stateCombo'><option>"
                                + "Select State</option></select>" );
 
            // disable add location button so that we don't get
            // more than one drop-down
            $(this).attr("disabled", "disabled");
 
            // add some sample states
            var states = ["California", "Florida", "New York"];
            $.each(states, function(index, value){
                   $("[name='stateCombo']").append("<option value='" 
                                                   + index
                                                   + "'>" 
                                                   + value 
                                                   + "</option>");
            });
 
            // add another empty select list
            $("body").append("<select name='cityCombo'>" 
                             + "<option>Select City</option></select>");
        });
 
        // use .live() since states select box doesn't exist yet
        $("[name='stateCombo']").live("change", function(event){
            // get name of state and fill with some data
            var selectedState = $(this).val();
 
            var CA_Cities = ["San Francisco", "Los Angeles", "Mountain View"];
            var FL_Cities = ["Fort Lauderdale", "Miami", "Orlando"];
            var NY_Cities = ["New York", "Buffalo", "Ithica"];
            var cities = [];
 
            if(selectedState == 0){
               cities = $.extend([], CA_Cities); 
            } else if(selectedState == 1){
               cities = $.extend([], FL_Cities); 
            } else if(selectedState == 2){
               cities = $.extend([],NY_Cities); 
            }
 
            // clear cityCombo of any previous values
            $("[name='cityCombo']").empty();
            $.each(cities, function(index, value){
                $("[name='cityCombo']").append("<option value='"
                                               +index
                                               +"'>"
                                               +value
                                               +"</option>"); 
            });
        });
      });
    </script>
  </head>
  <body>
    <div id="msg"></div>
    <form name="userRegistrationForm">
      <label for="userName">User</label>
      <input type="text" name="userName" /><br/>
      <label for="email">Email</label>
      <input type="text" name="email" /><br/>
      <label for="password">Password</label>
      <input type="password" name="password" /><br/>
      <label for="chkPassword">Re-enter Password</label>
      <input type="text" name="chkPassword" /><br/>
      <input type="button" name="validate" value="Validate Inputs" />
    </form>
    <input type="button" name="addLocation" value="Add Location" />
  </body>
</html>


以上只是冰山一角,详细内容,请参考jQuery帮助文档
(完...待续)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//判断元素是否高亮,初始值为-1,表示未有元素高亮 var higthlight=-1; var timeOutId; $(document).ready(function(){ //让div隐藏起来 var autoNode=$("#auto").css("border","2px gray none").height("200px").width("150px"); autoNode.hide(); var inputNode=$("#word"); //给文本框注册键盘事件 inputNode.keyup(function(event){ //处理键盘事件 var myEvent =event||window.event; var kcode=myEvent.keyCode; if(kcode >= 65 && kcode <= 90 || kcode==8 || kcode==46){ //把文本框中的内容取到 var wordText=inputNode.val(); //把数据发送到服务器 if(wordText!=""){ //清除定时器id clearTimeout(timeOutId); //设定定时器,延迟500ms发送 timeOutId=setTimeout(function(){ $.post("autoCompleteServlet",{word:wordText},function(data){ //把数据转换成jquery对象 var jqueryDom=$(data); //取所有的word节点 var wordNodes=jqueryDom.find("word"); //清空div中的内容 autoNode.html(""); //对word进行遍历 wordNodes.each(function(i){ //取当前word var wordNode=$(this); //创建div节点 var crediv=$("<div>").css("cursor","pointer").attr("id",i); //将内容加入div中 crediv.html(wordNode.text()); //鼠标进入时高亮选中内容 crediv.mouseover(function(){ if(higthlight!=-1){ $("#auto").children("div").eq(higthlight) .css("background-color","white"); } higthlight=$(this).attr("id"); $(this).css("background-color","gray"); }); crediv.mouseout(function(){ $(this).css("background-color","white"); }); crediv.click(function(){ var HightText=$(this).text(); higthlight==-1; $("#word").val(HightText); $("#auto").hide(); }); //将创建 的div加进去 autoNode.append(crediv); }); //如果服务器有数据返回,则让div显示 if(wordNodes.length>0){ var offset=inputNode.offset(); autoNode.show().css("position","absolute").css("left",offset.left+(-2)+"px").css("top",offset.top+$("#word").height()+6+"px"); }else{ autoNode.hide(); //所有都隐藏了,也就没有高亮元素,恢复初始值 higthlight=-1; } },"xml"); },500); }else{ autoNode.hide(); higthlight=-1; } }else if(kcode==38||kcode==40){ //如果输入的是向上(38)键或向下(40)键 //向上移动 if(kcode==38){ //取得auto的div元素下的所有div var autoNodes=$("#auto").children("div"); if(higthlight!=-1){ //如果以前有元素被 高亮了,则将其它颜色改为白色 autoNodes.eq(higthlight).css("background-color","white"); higthlight--; }else{ higthlight=autoNodes.length-1; } if(higthlight==-1){ higthlight=autoNodes.length-1; } autoNodes.eq(higthlight).css("background-color","gray"); } if(kcode==40){ //向下移动 //取得auto的div元素下的所有div var autoNodes=$("#auto").children("div"); if(higthlight!=-1){ //如果以前有元素被 高亮了,则将其它颜色改为白色 autoNodes.eq(higthlight).css("background-color","white"); } higthlight++; if(higthlight==autoNodes.length){ higthlight=0; } autoNodes.eq(higthlight).css("background-color","gray"); } }else if(kcode==13){ //下拉框有高亮内容 if(higthlight!=-1){ var HightText=$("#auto").hide().children("div").eq(higthlight).text(); higthlight==-1; $("#word").val(HightText); }else{ //下拉框没有高亮内容 sub(); $("#auto").hide(); $("#word").get(0).blur(); } } }); //给button注册个事件 $("input[type='button']").click(sub); }); function sub(){ alert("文本框中的["+$("#word").val()+"],被提交了"); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值