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;}
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"); }); });
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});
$("#CheckedRev").click(function(){ $('[name=items]:checkbox').each(function(){ $(this).attr("checked", !$(this).attr("checked")); //反选 }); });
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'); //追加给对方})
5. Form verification
Verify user name:
if($(this).is('#username')){ if(this.value==""||this.value.length<6){ var errorMsg='请输入至少6位的用户名'; $parent.append('<span clsaa="formtips onError">'+errorMsg+'</span>'); }else{ var okMsg='输入正确'; $parent.append('<span class="formtips onSuccess">'+okMsg+'</span>'); } }
The same applies to verifying the email address;
Submission event:
$('#send').click(function(){ $("form.required:input").trigger('blur'); var numError=$('form.onError').length; if(numError){ return false; } alert("注册成功,密码已发到你的邮箱,请查收"); });
2. Table application
1. Table color change
Ordinary alternate row color change:
$(function(){ $("tbody>tr:odd").addClass("odd"); //给表格中奇数行添加样式 $("tbody>tr:even").addClass("even"); //给表格中偶数行添加样式})
The radio button controls the highlighting of alternate rows in the table:
$('tbody>tr').click(function(){ $(this) .addClass('selected') //给单击的当前行添加高亮样式 .siblings().removeClass('selected') //将兄弟行的高亮模式去掉,执行完对象变为$(this).sibling() .end() //返回$(this)对象 .find(':radio').attr('checked',true); //将此行所在的单选框也选中});
2. Table expansion and closing
$(function(){ $('tr.parent').click(function(){ //获取所谓的父行 $(this) .toggleClass("selected") //添加/删除高亮 .siblings('.child_'+this.id).toggle(); //隐藏/显示所谓的子行 }).click(); //当用户刚进入界面时默认收缩起来})
3Table content filtering
$(function(){ $("#filterName").keyup(function(){ //给文本框绑定触发事件 $("table tbody tr").hide() .filter(":contains('"+($(this).val())+"')").show(); //根据文本框的输入筛选出行中有val值的行 }); });
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!