2011-11-06 49 views
2

comboboxselect事件定義:如何在選擇函數中獲取組合框值?

$.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.valueselect函數獲取組合框的值,但如果我嘗試通過組合框的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/

+0

對於已選擇的項目,可以使用'$('#cbCountry')。next()。val()'。 – hyperslug

+0

@hyperslug,它的工作原理,但看起來像從輸入獲取值,而不是從選擇。所以它會返回輸入的文本值,而不是選擇選項值。 –

+0

爲什麼不把全文存儲在'success'回調中:'value:item.label'? – hyperslug

回答

0

您必須遍歷組合框中選定的每個項目。例如:

var test_arr = new Array(); 
    $("#test1 option:selected").each(function() { 
     test_arr.push($(this).val()); 
    }); 

Fiddle

希望這有助於!

+0

看起來像jsfiddle鏈接不正確。而且我不認爲這樣的方法會幫助我,因爲實際上我沒有添加任何「選項」(值從遠程源讀取並且<選項...不會添加到頁面中) –

相關問題