2010-03-30 80 views
1

我正在使用自動填充窗口小部件,並希望在選擇某個項目時顯示對話框。該對話框顯示出來,但我想讓對話框中的一個字段在打開時獲得焦點。以下是我迄今爲止嘗試:jquery ui 1.7選擇自動完成顯示對話框

// HTML

<form action="#"> 
    <p><input id="busca" /></p> 
</form> 
<div id="agregar" title="Agregar Parte"> 
    <label for="cantidad">Cantidad:</label> 
    <input name="cantidad" id="cantidad" size="3" /> 
</div> 

// jQuery的

$(function(){ 

     $("#agregar").dialog({ 
         autoOpen: false, 
         //also tried open: function(){$("#cantidad").focus()} 
         } 
      );  
     //.bind("dialogfocus", ...) does not work either 
     $("#agregar").bind("focus", function(){ 
        $("#cantidad").focus(); }); 

    $("#busca").autocomplete({ 
      source: "/carrito/autocomplete/", 
      minLength: 1, 
         select: function(e, ui) { 
          $("#agregar").dialog("open"); 
         } 
     }); 


     }); 

我覺得自選的默認行爲,又是做什麼的自選小部件後獲得焦點會顯示對話框。

任何幫助將不勝感激。

回答

1

試試這個覆蓋自動完成的行爲:

$("#agregar").dialog({ 
    autoOpen: false, 
    open: function(){ 
     setTimeout(function() { $("#cantidad").focus(); }, 0); 
    } 
}); 

此排隊.focus()選擇代碼運行後運行。這是自動完成正在做(straight from the source code, line 604):呼喚你的選擇(和open函數),然後偷焦點回:

select(); 
// TODO provide option to avoid setting focus again after selection? 
// useful for cleanup-on-focus 
input.focus(); 
+0

非常感謝你,現在的工作。 – 2010-03-30 19:35:30