【问题标题】:cityname displaying using javascript autocomplete使用 javascript 自动完成显示城市名称
【发布时间】:2018-06-04 20:03:43
【问题描述】:

代码:

 $.ajax({
        url : '/Addbus',
        type : 'POST',
        dataType : 'JSON',
        success : function(data){
        var dt = JSON.parse(data.body);
            $.each(dt,function(i,v){
               $.each(v,function(j,p){
             var arr={};
                 arr= p.name+ ',' +p.id;
                temp.push(arr);
            var result=JSON.stringify(temp);
         console.log(result);

      $("#source").autocomplete({
        source: result, // The source of the AJAX results
        minLength: 3, // The minimum amount of characters that must be typed before the autocomplete is triggered
        focus: function( event, ui ) { // What happens when an autocomplete result is focused on
            $("#source").val( ui.item.value );
            return false;
      },
      select: function ( event, ui ) { // What happens when an autocomplete result is selected
          $("#source").val( ui.item.value );
          $('#source-id').val( ui.item.label );
      }
  });

当我在文本框中选择 cityname 然后在 ui.item.valueui.item.label 我得到的值是 物品 : 标签 : “海得拉巴,6” 价值 : “海得拉巴,6”, 现在我怎样才能将它们分开并接受不同的值。例如我想要值作为 id=6 和 name=hyderabad。

【问题讨论】:

  • 你会得到像 , 这样的值,所以你可以用逗号分割它

标签: javascript jquery arrays node.js mongodb


【解决方案1】:
const name = ui.item.value.split(',')[0];
const id = ui.item.value.split(',')[1];

【讨论】:

  • 通过使用这个我得到的错误为 Uncaught TypeError: Cannot read property 'split' of undefined
【解决方案2】:

也许这会对你有所帮助,不要对结果数组进行字符串化,将其用作源中的数组

$.ajax({
        url : '/Addbus',
        type : 'POST',
        dataType : 'JSON',
        success : function(data){
        var dt = JSON.parse(data.body);
            $.each(dt,function(i,v){
               $.each(v,function(j,p){
             var arr={};
                 arr.label = p.name;
                 arr.value = p.id;
                temp.push(arr);
            var result=temp

      $("#source").autocomplete({
        source: result, // The source of the AJAX results
        minLength: 3, // The minimum amount of characters that must be typed before the autocomplete is triggered
        focus: function( event, ui ) { // What happens when an autocomplete result is focused on
            $("#source").val( ui.item.value );
            return false;
      },
      select: function ( event, ui ) { // What happens when an autocomplete result is selected
          $("#source").val( ui.item.value );
          $('#source-id').val( ui.item.label );
      }
  });

【讨论】:

  • 到目前为止还不错,但我的问题是如何将城市名称和 id 拆分为两个变量。它是一样的,我的输出如下 ["Elurupadu,190", "Sendhwa, 1230", "Multai,1262", "Kherwara,1480", "Mandi,2225", "Thamarassery,519", "Ayyampettai,208", "Udaipur,5519",],现在只选择城市名称应该显示和id 应该是隐藏值
  • 然后在源参数中放一个这样的数组
  • [ { 标签:“标签”,值:“值”}]
【解决方案3】:

尝试使用这个...

const name = ui.item.split(',')[0];

const id = ui.item.split(',')[1];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-08
    • 2018-06-02
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多