简体   繁体   English

jQuery:如何修改函数内部的变量

[英]jQuery: How to modify variable inside function

I have a autocomplete function that I use to help me add keywords faster when inserting rows into database.我有一个自动完成功能,用于帮助我在将行插入数据库时​​更快地添加关键字。 Inside function I have a variable in which I have stored all keywords used for suggestions.在函数内部,我有一个变量,其中存储了所有用于建议的关键字。 After a row is inserted to database, with AJAX I get all keywords from database including keyword from the row I just added.将一行插入到数据库后,使用 AJAX 我从数据库中获取所有关键字,包括我刚刚添加的行中的关键字。

The problem is that I want to modify the variable in which are stored the old keywords by adding the keywords I just got from database with AJAX.问题是我想通过添加我刚从数据库中使用 AJAX 获得的关键字来修改存储旧关键字的变量。

Here is the code:这是代码:

var autocompleteKeywords = [ 'keyword 1', 'keyword 2', 'keyword 3' ];

// Autocomplete tags
$('#search_keyword').autocomplete({
    maxHeight: 400,
    width: 200,
    lookup: autocompleteKeywords
});

The variable I want to modify with the new keywords I got with AJAX is autocompleteKeywords .我想用 AJAX 获得的新关键字修改的变量是autocompleteKeywords

Here is the AJAX:这是 AJAX:

$.ajax({
    url: "ajax_actions.php",
    type: "GET",
    data: { action: "select_keywords" },
    dataType: "json",
    async: false,
    
    success: function(result)
    {
        autocompleteKeywords = result.keywords;
    }
});

You don't need to handle the datasource yourself, there is an optition of jQuery UI Autocomplete that meet your need:您不需要自己处理数据源,有一个 jQuery UI 自动完成选项可以满足您的需求:

Have a look: http://jqueryui.com/autocomplete/#remote看看: http : //jqueryui.com/autocomplete/#remote

    $( "#birds" ).autocomplete({
      source: "search.php",
      minLength: 2,
      select: function( event, ui ) {
        log( ui.item ?
          "Selected: " + ui.item.value + " aka " + ui.item.id :
          "Nothing selected, input was " + this.value );
      }
    });

or here: http://jqueryui.com/autocomplete/#remote-jsonp或在这里: http : //jqueryui.com/autocomplete/#remote-jsonp

$( "#city" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: "http://ws.geonames.org/searchJSON",
      dataType: "jsonp",
      data: {
        featureClass: "P",
        style: "full",
        maxRows: 12,
        name_startsWith: request.term
      },
      success: function( data ) {
        response( $.map( data.geonames, function( item ) {
          return {
            label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
            value: item.name
          }
        }));
      }
    });
  },
  minLength: 2,
  select: function( event, ui ) {
    log( ui.item ?
      "Selected: " + ui.item.label :
      "Nothing selected, input was " + this.value);
  },
  open: function() {
    $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
  },
  close: function() {
    $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
  }
});

Assuming from your description you want to add the keywords coming from the database假设根据您的描述,您要添加来自数据库的关键字

The best way in my opinon is to concatenate the default array with the incoming values from the database我认为最好的方法是将默认数组与来自数据库的传入值连接起来

success: function(result) {
    autocompleteKeywords = [].concat(autocompleteKeywords, result.keywords)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM