2013-06-21 32 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