2013-04-09 86 views
0

你好傢伙我一直在使用jquery自動完成組合框,一切工作正常,直到我不得不以編程方式更改下拉列表的索引。通過代碼的jquery組合框更新索引不更新組合框

看來,我將不得不修改這個

select: function (event, ui) { 
       ui.item.option.selected = true; 
       self._trigger("selected", event, { 
        item: ui.item.option 
       }); 

,讓我通過代碼更新索引但林不知道怎麼做,能有人伸出援助之手我怎麼會acomplish說。非常感激任何的幫助。 此處顯示組合框代碼。 http://jqueryui.com/autocomplete/#combobox 在此感謝您提供的任何幫助。

我嘗試了這幾種不同的方式,它只有當我回到非jQuery解決方案時才起作用。

 $("#StartLocations").val($("#EndLocations option:selected").val()); 
     $("#EndLocations").prop("selectedIndex", 1); 
     $("#EndLocations").val(1); 

:更新

function RunDirBtn_onclick(r){ 
     if (r == 'Y') { 
      index = EndLocations.selectedIndex; 
      StartNode = MystartingLocation[index].locNode; 

      index = StartLocations.selectedIndex; 
      EndNode = MystartingLocation[index].locNode; 

      // $("#StartLocations").val($("#EndLocations option:selected").val()); 
      // $("#EndLocations").prop("selectedIndex", 1); 
      // $("#EndLocations").val(1); 
      var startint = $("#StartLocations").attr("selectedIndex"); 
      var endint = $("#EndLocations").attr("selectedIndex"); 

      $("#EndLocations").attr("selectedIndex", startint); 
      $("#StartLocations").attr("selectedIndex", endint); 

     } else { 
      index = EndLocations.selectedIndex; 
      EndNode = MystartingLocation[index].locNode; 

      index = StartLocations.selectedIndex; 
      StartNode = MystartingLocation[index].locNode; 
     } 

我可以改變索引每個dropdpown我唯一的問題是,我不能與自動完成組合框使用它。 組合框代碼

$.widget("ui.combobox", { 
    _create: function() { 
     var self = this, 
      select = this.element.hide(), 
      selected = select.children(":selected"), 
      value = selected.val() ? selected.text() : "Select One..."; 
     var input = this.input = $("<input>") 
      .insertAfter(select) 
      .val(value) 
      .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; 
        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) { 
          $(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); 
     }; 

     this.button = $("<button type='button'>&nbsp;</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() { 
       if (input.autocomplete("widget").is(":visible")) { 
        input.autocomplete("close"); 
        return; 
       } 

       $(this).blur(); 
       input.autocomplete("search", ""); 
       input.focus(); 
      }); 
    }, 

    destroy: function() { 
     this.input.remove(); 
     this.button.remove(); 
     this.element.show(); 
     $.Widget.prototype.destroy.call(this); 
    } 
}); 
+0

在jQuery UI組合框中沒有選擇事件,該代碼片段用於自動完成事件。 – ricardohdz 2013-04-09 21:25:24

+0

對不起,我是一個白癡,我真的在試圖弄清楚這一點。我很抱歉。 – Miguel 2013-04-09 21:27:50

回答

0

我認爲這個問題是自動完成的組合框沒有任何屬性爲「的selectedIndex」。

您可能希望該屬性添加到自動完成,如果你想實現的是:

var startint = $("#StartLocations").attr("selectedIndex"); 
var endint = $("#EndLocations").attr("selectedIndex"); 

$("#EndLocations").attr("selectedIndex", startint); 
$("#StartLocations").attr("selectedIndex", endint); 
+0

謝謝,但沒有解決它。 – Miguel 2013-04-09 21:38:16

+0

你能分享更多信息/代碼片段嗎? – ricardohdz 2013-04-09 21:40:38

+0

當然。現在更新帖子。 – Miguel 2013-04-09 21:43:10