When doing the search function, I often encounter the need to check the input box. The most common one is instant search, so I will summarize it today.
Solutions for instant search:
(1) change event The trigger event must meet two conditions:
propertychange, whenever the current object property changes.
For example, a drop-down box appears after entering text, as shown in the figure:
When the content in the input box is empty, the drop-down box needs to be hidden, which can be achieved through the input and propertychange methods. The code is as follows:
- <script>
- $("#search").bind("input propertychange",function(){
- var value=$(this).val();
- if(value){
- $(".pc_search ul").show();
- }else{
- $(".pc_search ul").hide();
- }
- });
- </script>
Here bind binds both input and propertychange methods.
======================================================
Notice:
As of jQuery 1.7, on()
functions provide all the functionality needed to bind event handlers and are used to uniformly replace the previous event functions such as bind(), delegate(), live(), etc.
// jQuery 1.0+ (1.4.3+ supports parameter data) $ ( "selector" ). click ( [ data ,] handler ); // jQuery 1.0+ (1.4.3+ supports parameter data) $ ( "selector" ). bind ( "click" [, data ], handler ); // jQuery 1.3+ (1.4+ supports parameter data) $ ( "selector" ). live ( "click" [, data ], handler ); // jQuery 1.4.2+ $("ancestor").delegate("selector","click"[, data ], handler ); // jQuery 1.7+ $("ancestor").on("click","selector"[, data ], handler );
Project use:
$("#buildYear").on('input propertychange',function () { var value=$(this).val(); if(value>=1900&&value<=2017){ $("a.ssq-error.buildYear").css({"padding-left":"0px"}); $("a.ssq-error.buildYear").html(""); $("#evaluate").removeAttr("disabled"); }else{ $("a.ssq-error.buildYear").css({"padding-left":"12px"}); $("a.ssq-error.buildYear").html("年代取值范围:1900-2017"); } });