dsznndq4912405 2016-04-15 12:04
浏览 36

使jQuery自动完成结果,链接

I'm using jQuery auto-complete, bundled with jquery-ui. Any way, I need to customize this little bit to pop-up links instead of just texts.

I have a multi dimensional PHP array which contains some texts and corresponding id of that text in MYSQL database.

$js_array = json_encode($php_array);
echo "var javascript_array = ". $js_array . ";
";

So, now I have a multidimensional js array. But I have no idea how to use those values to create links.

The text items in the array should be the text part of the links, and the IDs should be the URL of the links.

This is my existing code. How to customize this to achive my puurpose...

$("#search_query").autocomplete( { 
   source: javascript_array
});
  • 写回答

2条回答 默认 最新

  • douhuang2673 2016-04-15 12:11
    关注

    Use the _renderItem option:

    Method that controls the creation of each option in the widget's menu. The method must create a new <li> element, append it to the menu, and return it.

    _renderItem: function( ul, item ) {
      return $( "<li data-value='"+item.value+"'><a href='#'>"+item.label+"</a></li>" )
        .appendTo( ul );
    }
    

    1) Remove this:

    .autocomplete( "instance" )._renderItem = function( ul, item ) {
      return $( "<li>" )
        .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
        .appendTo( ul );
    };
    

    2) Add it up here instead. Make sure you have a href parameter or most browsers won't knowledge it as a link.

    $( "#project" ).autocomplete({
          minLength: 0,
          source: projects,
          focus: function( event, ui ) {
            $( "#project" ).val( ui.item.label );
            return false;
          },
          select: function( event, ui ) {
            $( "#project" ).val( ui.item.label );
            $( "#project-id" ).val( ui.item.value );
            $( "#project-description" ).html( ui.item.desc );
            $( "#project-icon" ).attr( "src", "images/" + ui.item.icon );
    
            return false;
          },
          _renderItem: function( ul, item ) {
              return $( "<li data-value='"+item.value+"'><a href='#'>"+item.label+"</a></li>" )
                   .appendTo( ul );
           }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?