jQuery Auto Complete ComboBoxでデフォルト値をどのように設定しますか?
-
02-10-2019 - |
質問
使用するとき jQuery UIオートコンプリート コンボボックス, 、コンボボックスのデフォルト値を設定できますか?
解決
私は自分のプロジェクトでそれをするようにこれに答えてみました。
投稿したページからデモソースコードを読みます。 AutoCompleteコンボボックスを生成するjQueryコードでは、「選択」要素から選択した値を読み取るコンボボックスが作成されたときに処理するコードの1行を追加しました。このようにして、デフォルト値をプログラムで設定できます(AutoCompleteコンボボックスを使用していない場合に通常そうするように)
これが私が追加した1行です:
input.val( $("#combobox option:selected").text());
簡潔でシンプル。 #comboboxから選択した要素のテキスト値に入力の値を設定します。当然のことながら、個々のプロジェクトまたはページに一致するようにID要素を更新する必要があります。
ここでそれは文脈にあります:
(function($) {
$.widget("ui.combobox", {
_create: function() {
var self = this;
var select = this.element.hide();
var input = $("<input>")
.insertAfter(select)
.autocomplete({
source: function(request, response) {
var matcher = new RegExp(request.term, "i");
response(select.children("option").map(function() {
var text = $(this).text();
if (this.value && (!request.term || matcher.test(text)))
return {
id: this.value,
label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
value: text
};
}));
},
delay: 0,
change: function(event, ui) {
if (!ui.item) {
// remove invalid value, as it didn't match anything
$(this).val("");
return false;
}
select.val(ui.item.id);
self._trigger("selected", event, {
item: select.find("[value='" + ui.item.id + "']")
});
},
minLength: 0
})
.addClass("ui-widget ui-widget-content ui-corner-left");
// This line added to set default value of the combobox
input.val( $("#combobox option:selected").text());
$("<button> </button>")
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.insertAfter(input)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
}).removeClass("ui-corner-all")
.addClass("ui-corner-right ui-button-icon")
.click(function() {
// close if already visible
if (input.autocomplete("widget").is(":visible")) {
input.autocomplete("close");
return;
}
// pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
input.focus();
});
}
});
})(jQuery);
他のヒント
に基づく マシュー・スティールの答え, 、これを使用する代わりに:
input.val( $("#combobox option:selected").text());
私はこれを使用します:
input.val( $(select).find("option:selected").text());
ウィジェットは再利用可能で乾燥しています:)
回答#1は非常に近いですが、関数をジェネリックに保ちたい場合、要素IDをハードコードすることはできません。代わりにこのラインを追加して楽しんでください!
input.val(jQuery("#"+select.attr("id")+" :selected").text() );
これは、自動コンプターの次のステートメントを編集することで実現できます。
value = selected.val() ? selected.text() : "Select Institution";
ここで応答を調整して、Comboboxで既に定義されている選択変数を使用して選択したオプションを見つけて使用します。これは、コンボボックスを(ID、クラス、またはセレクターを使用して)定義した要素がどの汎用であり、複数の要素でも機能することを意味します。
input.val(select.find("option:selected").text());
これが誰かを助けることを願っています!
jQueryコンボボックススクリプトにメソッドを追加します
setValue: function(o) {
$(this.element).val(o);
$(this.input).val($(this.element).find("option:selected").text());
}
電話してください option
初期化した後にボックスの値を設定する方法。
$('#combo').autocomplete( "option" , optionName , [value] )
これがあなたを助けるかもしれません
input.val( $(select).children("option:selected").text());
代わりに以下のコード行を使用しましたが、同じ結果を達成する別の方法です:)
$('option:selected', this.element).text()
function setAutoCompleteCombo(id,value,display) {
if($(id+"[value="+value+"]").text().localeCompare("")==0){
$("<option value='"+value+"'>"+display+"</option>").appendTo($(id));
}
$(id).val(value);
$(id).next().val($(id+" :selected").text());
}
この関数を最初のページまたはランタイムで呼び出すことで解決しました。例:
setAutoCompleteCombo('#frmData select#select_id',option_value,option_text);
jQuery UIコンボボックスの実装に役立つ答えがあります。コードの途中のどこかがこれでした:
.val(value)
私はそれを変更しました:
.val(select.val())
そして、Presto、基礎となるテキストボックスの初期値が表示されました。これはデフォルトの機能であるべきだと思われますが、何を知っていますか?
前に1つのコードを下に追加する」 this._on( this.input, {
" ライン
this.input[0].defaultValue = value;
AutoComplete ComboBoxスクリプトでコードを作成した後。 HTMLのリセットボタンでリセットすることもできます。
このエラーを困惑させ、コード化したものは何でもオンロードイベントを修正できません(おそらく3時間)。ついに幸運なことに私は全面的になりました http://www.onemoretake.com/2011/04/17/a-better-jquery-ui-combo-box/ Webページ(頭痛を解決するための@Danのthx)と、UI定義関数自体では、実際の欠落部分が「オンロード」にないのを見ました。公式jQuery UI Webページのスタンドアート定義関数には、プログラムで選択されたオプションが含まれていません。
以下は、定義関数に追加する機能です。
//allows programmatic selection of combo using the option value
setValue: function (value) {
var $input = this.input;
$("option", this.element).each(function () {
if ($(this).val() == value) {
this.selected = true;
$input.val(this.text);
return false;
}
});
}
また、オプションテキストではなくオプションテキストを介して選択を変更するための別の関数を作成しました
//allows programmatic selection of combo using the option text
setValueText: function (valueText) {
var $input = this.input;
var selectedText;
$("option", this.element).each(function () {
if ($(this).text() == valueText) {
this.selected = true;
$input.val(this.text);
return false;
}
});
}
これらの関数は、オンロードまたは別の関数で以下を使用できます。
$("#yourComboBoxID").combobox("setValue", "Your Option Value");
また
$("#yourComboBoxID").combobox("setValueText", "Your Option Text");