2013-06-21 40 views
0

我有一個窗體打開在對話框中。其中一個字段具有自動完成功能。所有的字段都會生成,並且服務器值將存儲在其中以預填表格。帶對話框更新值的jquery自動完成

var mydiv = jQuery("#editform"); 
var $myform = jQuery("<form id='EditForm' method='post' action='index.php?option=com_component&task=edit'></form>"); 
... 
var $mylabel10 = jQuery("<label for='EditSelect'>A label</label>"); 
var $myinput9 = jQuery("<input id='EditSelect' name='EditSelect' type='text' />"); 
var $mylabel9 = jQuery("<label for='EditSelect2'>Another label</label>"); 
var $myinput8 = jQuery("<input id='EditSelect2' name='add_path' value='" +path + "' />"); //path is a value passed in from the server 

$myform.append(... $mylabel10, $myinput9, $mylabel9, $myinput8); 
mydiv.append($myform); 

    //autocomplete code - order is important to have autocomplete go outside dialog 
    var available = [ 
      { label : 'foo', value : 'bar' }, 
      { label : 'xyz', value : 'abc' }, 
      ... 
     ]; 
    jQuery("#EditSelect", mydiv).autocomplete({ 
     source: available, 
     focus : function(){ return false; } 
    }) 
    .on('autocompleteselect', function(e, ui){ 
     var t = jQuery(this), 
      details = jQuery('#EditSelect2'), 
      label = (e.type == 'autocompleteresponse' ? ui.content[0].label : ui.item.label), 
      value = (e.type == 'autocompleteresponse' ? ui.content[0].value : ui.item.value); 

     t.val(label); 
     details.val(value); //doesn't update the form here? 

     return false; 
    }); 

    // get reference to autocomplete element 
    var autoComplete = jQuery("#EditSelect", mydiv).autocomplete("widget"); 

    var dialogOpts = { 
      modal: true, 
      autoOpen: true, 
      resizable: false, 
      width: 525, 
      height: 'auto', 
      title: 'Edit settings' 
     }; 

    mydiv.dialog(dialogOpts); 
    autoComplete.insertAfter(mydiv.parent()); 

在該編輯對話框是在被選擇時應該更新其他輸入字段(#EditSelect2)自動完成。目前#EditSelect2具有來自服務器的值(在變量path中)。

當從自動完成選擇一個新值我希望的,因爲這個代碼更新的形式:details.val(value);。現在自動完成功能正常,但是在自動完成中選擇新選項後,服務器的值(path)不會更新。

希望這是有道理的。

回答

1

。在你的myinput8語句一個小語法錯誤:

$myinput8 = jQuery("<input id='EditSelect2' name='add_path' value='" +path + " />"); 

應該是:

$myinput8 = jQuery("<input id='EditSelect2' name='add_path' value='" +path + "' />"); 

注意在年底額外的單引號。

+0

感謝尋找,但是當我刪除的問題不相關的其他垃圾,這是隻是一個問題的剪切/粘貼錯誤。 – Tom

+0

@湯姆:但在此修復這個小提琴後:http://jsfiddle.net/3FJuC/似乎工作(使用FF21)。 – mhu

+0

感謝您堅持使用它,並向我展示它確實奏效。我有重複IDS ...嘆了口氣。 – Tom

相關問題