2014-01-16 56 views
0

我有一個自動完成/基本的表單,按預期工作(我想)。我輸入三個字符,得到匹配等。 當我選擇一個匹配時,無論是用鍵(並用Enter確認)或鼠標單擊它需要另一個回車提交表單。這感覺...不自然:-)ATK4自動完成提交表單上選擇或輸入

所以我試圖讓選擇後,自動完成/基本表單字段提交表單。

我的第一個(誤導)測試涉及嘗試做線沿線的東西:

$form->getElement('fieldname')->js('select', $form->js()->univ()->alert('Select!')); 

該火災時,在表單字段中選擇文本,不完全是我一直在尋找。

我開始看着autocomplete_univ.js和(unsuccesfully)添加$(other_field).closest('form')。submit(); 但我完全不瞭解jQuery/JS,所以我希望有人能幫助我。

關於如何觸發(提交)自動完成選擇的任何想法?

this.jquery.autocomplete($.extend({ 
     source: data, 
     focus: function(event, ui) { 
      // Imants: fix for item selecting with mouse click 
      var e=event; 
      while(e.originalEvent!==undefined) e=e.originalEvent; 
      if(e.type!='focus') q.val(ui.item[title_field]); 

      return false; 
     }, 
     select: function(event, ui) { 
      q.val(ui.item[title_field]); 
      $(other_field).val(ui.item[id_field]); 
      //Added this line below 
      $(other_field).closest('form').submit(); 

      return false; 
     }, 
     change: function(event, ui) { 
      var data=$.data(this);//Get plugin data for 'this' 
      if(data.uiAutocomplete.selectedItem==undefined) { 
       if("mustMatch" in options) q.val(''); 
       $(other_field).val(q.val()); 

       return false; 
      } 
     } 
    },options)) 
    .data("ui-autocomplete")._renderItem = function(ul, item) { 
     return $("<li></li>") 
      .data("ui-autocomplete-item", item) 
      .append("<a>" + item[title_field] + "</a>") 
      .appendTo(ul); 
    }; 
+0

沒有更多閱讀: $(other_field).VAL(ui.item [id_field]); 改爲 $(other_field).val(ui.item [id_field] .val); 觸發提交像我想要的。除了現在我所需要的值不再被傳遞。 任何人都可以向我解釋爲什麼並建議修復? –

+0

我改回$(other_field).val(ui.item [id_field]);我不知道爲什麼 - 但現在看起來像預期的那樣工作,在需要時通過提交id來提交。 有人可以確認上述代碼的工作原理如下:提交表單並通過鼠標選擇和按鍵傳遞id? 謝謝 –

回答

0

我想你在ATKs JS庫中的chane是不正確的。

可能應該工作的是鉤住other_field onChange事件。 事情是這樣的:

$(other_field).change(
    function(){$(other_field).closest('form').submit();} 
);