jQuery自动完成与对话框更新值 [英] jquery autocomplete with dialog updating value
问题描述
我有一个在对话框中打开的表单.其中一个字段具有自动完成功能.构建所有字段并将服务器值存储在其中,以预先填写表单.
I have a form that opens up in a dialog. One of the fields has autocomplete. All fields are built and server values are stored in them to pre-fill the form.
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
中).
In this edit dialog is an autocomplete that when selected it should update the other input field (#EditSelect2
). Currently #EditSelect2 has the value from the server (in the variable path
).
当从自动完成功能中选择一个新值时,由于以下代码,我希望表单被更新:details.val( value );
.现在,自动完成功能可以正常工作,但是在自动完成功能中选择新选项后,服务器(path
)的值不会得到更新.
When a new value is selected from the autocomplete I would expect the form updated because of this code: details.val( value );
. Right now the autocomplete works fine but the value from the server (path
) doesn't get updated after selecting a new choice in the autocomplete.
希望这是有道理的.
推荐答案
myinput8语句中有一个小的语法错误:
There is a small syntax error in your myinput8 statement:
$myinput8 = jQuery("<input id='EditSelect2' name='add_path' value='" +path + " />");
应为:
$myinput8 = jQuery("<input id='EditSelect2' name='add_path' value='" +path + "' />");
请注意在末尾加上多余的单引号.
Note the extra single quote at the end.
这篇关于jQuery自动完成与对话框更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!