Home > Web Front-end > JS Tutorial > body text

How to operate forms and tables with jQuery

零到壹度
Release: 2018-03-17 15:45:30
Original
1358 people have browsed it

This time I will show you how to use jQuery to operate forms and tables and some other applications. Let’s follow the editor and take a look.

1. Form application

A form has three basic components:

(1) Form tag: Contains the server side used to process form data The program URL and method of submitting data to the server.
(2) Form fields: including text boxes, password boxes, hidden fields, multi-line text boxes, check boxes, radio button boxes, drop-down selection boxes and text upload boxes, etc.
(3) Form buttons: including submit buttons, reset buttons and general buttons, used to transfer data to the server or cancel the transfer, and can also be used to control other processing tasks with defined processing scripts.
1. Single-line text box application
When the text box gains focus, its color needs to change. When it loses focus, it needs to be restored to its original style. You can use the pseudo style in css. Class selector to achieve the above functions, the css code is as follows:

input:focus ,textarea:focus{ 
   border:1px solid #f00;  
     background:#fcc;}
Copy after login

However, IE6 does not support the :hover pseudo-class selector other than hyperlink elements. In this case, you can use jQuery to make up for it:

.focus{
    border:1px solid #f00;
    background:#fcc;
}
$(function(){
    $(":input").focus(function(){
        $(this).addClass("focus");
    }).blur(function(){
        $(this).removeClass("focus");
    });
});
Copy after login

2. Multi-line text box application
Height change: Bind click events through the "zoom in" and "zoom out" buttons, and the height of the corresponding text box will also be enlarged or reduced.
Scroll bar height change: Bind click events through the "Up" and "Down" buttons
3. Check box application

$("#CheckedAll").click(function(){
    $('[name=items]:checkbox').attr('checked',true);  //复选框全选,全不选设置为false});
Copy after login
$("#CheckedRev").click(function(){
    $('[name=items]:checkbox').each(function(){
        $(this).attr("checked", !$(this).attr("checked"));  //反选
    });
});
Copy after login

4. Drop-down box application
Add the options from the left border to the right border:

('#add').click(function(){
    var $options=$('#select1 options:selected');  //获取全部的选项
    $options.appendTo('#select2');  //追加给对方})
Copy after login

5. Form verification
Verify user name:

if($(this).is('#username')){  
  if(this.value==""||this.value.length<6){     
     var errorMsg=&#39;请输入至少6位的用户名&#39;;
        $parent.append(&#39;<span clsaa="formtips onError">&#39;+errorMsg+&#39;</span>&#39;);
    }else{      
      var okMsg=&#39;输入正确&#39;;
        $parent.append(&#39;<span class="formtips onSuccess">&#39;+okMsg+&#39;</span>&#39;);
    }
}
Copy after login

The same applies to verifying the email address;
Submission event:

$(&#39;#send&#39;).click(function(){
    $("form.required:input").trigger(&#39;blur&#39;);  
      var numError=$(&#39;form.onError&#39;).length; 
         if(numError){    
             return false;
    }
    alert("注册成功,密码已发到你的邮箱,请查收");
});
Copy after login

2. Table application
1. Table color change
Ordinary alternate row color change:

$(function(){
    $("tbody>tr:odd").addClass("odd");  //给表格中奇数行添加样式
    $("tbody>tr:even").addClass("even"); //给表格中偶数行添加样式})
Copy after login

The radio button controls the highlighting of alternate rows in the table:

$(&#39;tbody>tr&#39;).click(function(){
    $(this)
        .addClass(&#39;selected&#39;)   //给单击的当前行添加高亮样式
        .siblings().removeClass(&#39;selected&#39;) //将兄弟行的高亮模式去掉,执行完对象变为$(this).sibling()
        .end()  //返回$(this)对象
        .find(&#39;:radio&#39;).attr(&#39;checked&#39;,true); //将此行所在的单选框也选中});
Copy after login

2. Table expansion and closing

$(function(){
    $(&#39;tr.parent&#39;).click(function(){   //获取所谓的父行
        $(this)
        .toggleClass("selected")    //添加/删除高亮
        .siblings(&#39;.child_&#39;+this.id).toggle();  //隐藏/显示所谓的子行
    }).click();  //当用户刚进入界面时默认收缩起来})
Copy after login

3Table content filtering

$(function(){
    $("#filterName").keyup(function(){  //给文本框绑定触发事件
        $("table tbody tr").hide()  
        .filter(":contains(&#39;"+($(this).val())+"&#39;)").show(); //根据文本框的输入筛选出行中有val值的行
    });
});
Copy after login

The above is the detailed content of How to operate forms and tables with jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!