最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

使用javascript自动完成功能显示的城市名称

IT培训 admin 7浏览 0评论

使用javascript自动完成功能显示的城市名称

码:

 $.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中我得到的值为item:label:“Hyderabad,6”value:“Hyderabad,6”,现在我如何能够分离它们并采用不同的值。示例我希望值为id = 6和name = hyderabad。

回答如下:

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

$.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 );
      }
  });

使用javascript自动完成功能显示的城市名称

码:

 $.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中我得到的值为item:label:“Hyderabad,6”value:“Hyderabad,6”,现在我如何能够分离它们并采用不同的值。示例我希望值为id = 6和name = hyderabad。

回答如下:

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

$.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 );
      }
  });
发布评论

评论列表 (0)

  1. 暂无评论