javascript – 如何将id的动态值传递给我从json获得的jquery?

前端之家收集整理的这篇文章主要介绍了javascript – 如何将id的动态值传递给我从json获得的jquery?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我从json获取值并传递给自动完成搜索字段.

[{"id":1,"name":"JAVA"},{"id":2,"name":"cake PHP"},"id":3,"name":"Android"}]

例如,当我单击JAVA时,我想获得JAVA的ID,例如www.example.com/1

Jquery代码

最佳答案
如果要将对象数组用作源,则需要提供以下逻辑:

>用于匹配用户输入的对象属性
>用于显示匹配项的文本的属性
>用户选择项目时使用的属性

更多信息:

> @L_301_0@
> http://api.jqueryui.com/autocomplete/#method-_renderItem
> http://api.jqueryui.com/autocomplete/#event-select

var tags = [
  {"id":1,{"id":3,"name":"Android"}
];


$( "#search" ).autocomplete({
  source: function( request,response ) {
          var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ),"i" );
          response( $.grep( tags,function( item ){
              return matcher.test( item.name ); // match user request with item.name
          }) );
      },select: function( event,ui ) {
    event.preventDefault();
    $("#search").val( ui.item.name ); // display user selection in textBox

    console.log('selected: ' + JSON.stringify(ui) );
    console.log('execute window.location = "example.com/' + ui.item.id + '"'); // use id of the selected item to generate required URL
    
  }
});

// provide rendering logic for each matched item
$w = $( "#search" ).data("ui-autocomplete");
$w._renderItem = function( ul,item ) {
  //console.log(JSON.stringify(item) );
    return $( "

猜你在找的jQuery相关文章