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!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
