我combobox與select事件定義:如何在選擇函數中獲取組合框值?
$.widget("ui.combobox", {
_create: function() {
var self = this,
select = this.element.hide(),
selected = select.children(":selected"),
value = selected.val() ? selected.text() : "";
var input = this.input = $("<input>")
.insertAfter(select)
.val(value)
.autocomplete({
delay: 0,
minLength: 0,
source: ...
select: function(event, ui) {
//
},
})
我可以用ui.item.value
select
函數獲取組合框的值,但如果我嘗試通過組合框的ID來獲得相同的 - $("#cbCity").val()
,我總是收到初始值。 (?)我不能使用ui.item.value
這裏,因爲這個功能是3個組合框定義和選擇功能我需要得到值所有的人:
$("#cbCountry").val()+'-'+$("#cbCity").val()+'-'+$("#cbOrg").val()
請參閱演示here。
看來我不得不在選擇功能動態生成選擇選項:
<option value="value">label</option>
,然後將它們標記爲選擇,因爲它是在最初的代碼中完成:
select: function(event, ui) {
ui.item.option.selected = true;
self._trigger("selected", event, {
item: ui.item.option
});
}
但我不當然如何去做 - http://jsfiddle.net/and7ey/k2J5v/4/。
對於已選擇的項目,可以使用'$('#cbCountry')。next()。val()'。 – hyperslug
@hyperslug,它的工作原理,但看起來像從輸入獲取值,而不是從選擇。所以它會返回輸入的文本值,而不是選擇選項值。 –
爲什麼不把全文存儲在'success'回調中:'value:item.label'? – hyperslug