首先,我从 Jquery 开始,我的问题有点小菜一碟,但我没有找到解决方案。我得到了官方的组合框示例。我将其更改为使用 Json 源。但我想要组合框的 2 个实例,源 URL 不同。
(我想我不需要创建第二个 Combobox 函数来更改源,如果我错了告诉我)
这是我的代码:
<div id="etape1">
<div class="ui-widget">
<label>Veuillez selectionner une application: </label>
<select id="combobox_application">
<option value="">Select one...</option>
<option value="Chargement en cours">Chargement en cours</option>
</select>
</div>
<button id="create-user">Ajouter une application</button>
</div>
<div id="etape2" style="display:none">
<div class="ui-widget">
<label>Veuillez selectionner un scenario: </label>
<select id="combobox_scenario">
<option value="">Select one...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
</select>
</div>
编辑
感谢您的帮助,不幸的是它不起作用。我在这:
- 有一个 jquery-combobox.js(来源如下)
在我的主页上,得到了这个:
<script> $(function() { $('#combobox_application').combobox({ source: function(req,add) { $.ajax({ url: "?module=gestionApplication&action=getListeApplications", dataType:"json", success: function(data, textStatus, XMLHttpRequest){ alert(data); add(data); } }); }, selected: function(event, ui) { $("#etape2").css('display', 'block'); } }); $('#combobox_scenario').combobox({ source: function(req,add) { $.ajax({ url: "?module=gestionApplication&action=getListeScenarios", dataType:"json", success: function(data, textStatus, XMLHttpRequest){ alert(data); add(data); } }); } });
jquery-combobox.js:
-
(function( $ ) {
$.widget( "ui.combobox", {
_create: function() {
var input,
self = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "",
wrapper = this.wrapper = $( "<span>" )
.addClass( "ui-combobox" )
.insertAfter( select );
input = $( "<input>" )
.appendTo( wrapper )
.val( value )
.addClass( "ui-state-default ui-combobox-input" )
.autocomplete({
delay: 0,
minLength: 0,
// Need define source in my main js
//~ source: function( request, response ) {
//~ $.ajax({
//~ url: "?module=gestionApplication&action=getListeApplications",
//~ dataType: "json",
//~ success: function( data ) {
//~ response( $.map( data, function( item ) {
//~ return {
//~ label: item.nom,
//~ value: item.nom,
//~ option: this
//~ }
//~ }));
//~ }
//~ });
//~ },
select: function( event, ui ) {
ui.item.option.selected = true;
self._trigger( "selected", event, {
item: ui.item.option
});
},
change: function( event, ui ) {
if ( !ui.item ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "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
$( this ).val( "" );
select.val( "" );
input.data( "autocomplete" ).term = "";
return false;
}
}
}
})
.addClass( "ui-widget ui-widget-content ui-corner-left" );
input.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
$( "<a>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.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" );
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();
});
},
destroy: function() {
this.wrapper.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
})( jQuery );
但这不起作用,选择没问题,但来源似乎没有改变任何东西。