天天看點

jquery combobox下拉及異步加載資料

璁闆綍锛?

閫氬父浼氶亣鍒闆姞杞藉緢澶氭潯鏁版嵁鍒頒笅鎷夋涓紝閫氳繃ajax鐨勫紓姝ュ姞杞芥柟娉曞疄鐜幫紝鍙傝€僯query ui 瀹樼綉

瀹氫箟涓€涓猦tml鏍囩

鐩戞帶绫誨瀷<select id="s_type"><option value="1" selected="selected">RFID</option><option value="2">瑙嗛</option></select>
           

鍔犺澆js鍜宑ss鏂囦歡

<link rel="stylesheet" type="text/css" href="../jquery/css/jquery.ui.all.css" target="_blank" rel="external nofollow" >
<script type="text/javascript" src="../jquery/jquery-1.8.3.js"></script>
<script type="text/javascript" src="../jquery/jquery-ui.js"></script>
           

鑷畾涔塩ss鏍峰紡

.ui-combobox {
	position: relative;
	display: inline-block;
}

.ui-combobox-toggle {
	position: absolute;
	top: 0;
	bottom: 0;
	margin-left: -1px;
	padding: 0;
	/* adjust styles for IE 6/7 */
	*height: 1.7em;
	*top: 0.1em;
}

.ui-combobox-input {
	margin: 0;
	padding: 0.3em;
}
           

jquery combobox灏佽浠g爜锛屾潵鑷?jquery ui 瀹樼綉

/**
 * 鑷畾涔塽i combobox
 */

	(function( $ ) {
		$.widget( "ui.combobox", {
			_create: function() {
				var input,
					that = this,
					select = this.element.hide(),
					selected = select.children( ":selected" ),
					value = selected.val() ? selected.text() : "",
					wrapper = this.wrapper = $( "<span>" )
						.addClass( "ui-combobox" )
						.insertAfter( select );

				function removeIfInvalid(element) {
					var value = $( element ).val(),
						matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
						valid = false;
					select.children( "option" ).each(function() {
						if ( $( this ).text().match( matcher ) ) {
							this.selected = valid = true;
							return false;
						}
					});
					if ( !valid ) {
						// remove invalid value, as it didn't match anything
						$( element )
							.val( "" )
							.attr( "title", value + " 鏈壘鍒頒換浣曞尮閰嶉」" )
							.tooltip( "open" );
						select.val( "" );
						setTimeout(function() {
							input.tooltip( "close" ).attr( "title", "" );
						}, 2500 );
						input.data( "autocomplete" ).term = "";
						return false;
					}
				}

				input = $( "<input>" )
					.appendTo( wrapper )
					.val( value )
					.attr( "title", "" )
					.addClass( "ui-state-default ui-combobox-input" )
					.autocomplete({
						delay: 0,
						minLength: 0,
						source: function( request, response ) {
							var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
							response( select.children( "option" ).map(function() {
								var text = $( this ).text();
								if ( this.value && ( !request.term || matcher.test(text) ) )
									return {
										label: text.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>" ),
										value: text,
										option: this
									};
							}) );
						},
						select: function( event, ui ) {
							ui.item.option.selected = true;
							that._trigger( "selected", event, {
								item: ui.item.option
							});
						},
						change: function( event, ui ) {
							if ( !ui.item )
								return removeIfInvalid( this );
						}
					})
					.addClass( "ui-widget ui-widget-content ui-corner-left" );

				input.data( "autocomplete" )._renderItem = function( ul, item ) {
					return $( "<li>" )
						.data( "item.autocomplete", item )
						.append( "<a>" + item.label + "</a>" )
						.appendTo( ul );
				};

				$( "<a>" )
					.attr( "tabIndex", -1 )
					.attr( "title", "鏄劇ず鎵€鏈? )
					.tooltip()
					.appendTo( wrapper )
					.button({
						icons: {
							primary: "ui-icon-triangle-1-s"
						},
						text: false
					})
					.removeClass( "ui-corner-all" )
					.addClass( "ui-corner-right ui-combobox-toggle" )
					.click(function() {
						// close if already visible
						if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
							input.autocomplete( "close" );
							removeIfInvalid( input );
							return;
						}

						// work around a bug (likely same cause as #5265)
						$( this ).blur();

						// pass empty string as value to search for, displaying all results
						input.autocomplete( "search", "" );
						input.focus();
					});

					input
						.tooltip({
							position: {
								of: this.button
							},
							tooltipClass: "ui-state-highlight"
						});
			},

			destroy: function() {
				this.wrapper.remove();
				this.element.show();
				$.Widget.prototype.destroy.call( this );
			}
		});
	})( jQuery );
	
           

鍙渶鍦ㄩ〉闈㈠姞杞界殑鏃跺€欒皟鐢ㄥ嵆鍙紝瀹炵幇浜哻ombobox鐨剆elect浜嬩歡鐨勪嬌鐢紝鍚屾椂涔熷吋鍏蜂簡jquery autocomplete combobox鐨勬晥鏋滐紱

$(function() {
		$("#s_type").combobox({
			selected : function(event,ui){
				var flag = $("#s_type option:selected").val();//鑾峰彇褰撳墠閫変腑value
				if(flag==1){
					doSearch();
				}
				else{
					doSearch2();
				}
			}
		});
		
	});
           

鏁堟灉鍥撅細

jquery combobox下拉及異步加載資料
jquery combobox下拉及異步加載資料

瀹炵幇寮傛鍔犺澆鏁版嵁鐨勪唬鐮侊細

function loadCombobox(obj1,obj2){
		//鍔犺澆autocomplete
		$.ajax({
			type : "POST",
			url : url鍦闆潃,
			dataType : "json",
			cache : false,
			async : false,
			data : {
				"param1" : 杈撳叆鐨勫瓧绗﹀弬鏁?
			},
			success : function(json) {
				var data = eval(json);//json鏁扮粍

				var optionStr = '';
				for (var i = 0; i < data.length; i++) {
					optionStr += "<option value=\"" + data[i].id + "\" >"
							+ data[i].name + "</option>";
				}
				$("#" + obj2).html("<option value='-1'>--璇烽€夋嫨--</option>" + optionStr);
			}
		});
		$("#"+obj2).combobox();
	};
           

鍚庡彴杩斿洖json鏍煎紡鏁版嵁锛?

//鐢ㄤ簬寮傛ajax鍔犺澆绾胯礬涓嬫媺鍒楄〃
            String param1 = URLDecoder.decode(request.getParameter("param1") == null ? "-1" : request.getParameter("param1").trim(), "utf-8");
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            BaseService service = new BaseService();
            List list = service.getlist(Long.parseLong(param1));
            if (list != null)
            {
                for (int i = 0; i < list.size(); i++)
                {
                    Table t = (Table)list.get(i);
                    jsonObject.put("id", t.getId());
                    jsonObject.put("name", t.getName());
                    jsonArray.add(jsonObject);
                }
            }
            PrintWriter pw = null;
            StringBuffer sb = new StringBuffer();
            sb.append(jsonArray.toString());
            try
            {
                response.setContentType("text/html;charset=GBK");
                pw = response.getWriter();
                pw.write(sb.toString());
                pw.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
           

鏁堟灉鍚屼笂

繼續閱讀