jquery autocomplete with dialog updating value

故事扮演 提交于 2019-12-12 00:46:46

问题


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());

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).

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.

Hope this makes sense.


回答1:


There is a small syntax error in your myinput8 statement:

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

should be:

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

Note the extra single quote at the end.



来源:https://stackoverflow.com/questions/17242943/jquery-autocomplete-with-dialog-updating-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!