【问题标题】:Jquery multiple autocomplete fieldJquery多个自动完成字段
【发布时间】:2013-04-05 10:13:03
【问题描述】:

我有来自 jquery 网站的 jquery 自动完成功能。它在一个领域工作得很好。但是现在我想在其中添加具有不同值的不同字段,我该怎么做?我尝试了几种方法,但搞砸了整个系统。我的所有领域都在工作,但现在却没有。我需要给它一个新的函数名吗?我是这方面的新手。

我认为通过在顶部添加一个新字段和新 var 会起作用,但它没有

var projects = [
  {
    value: "CMPT101",
    label: "CMPT 101",
    desc: "Discrete Mathematics I"

  },


  var instr={
      value:"johnson "
      lable:"Johnson"
  }
]



select: function( event, ui ) {
        $( "#project" ).val( ui.item.label );
        $( "#instr" ).val( ui.item.label );
        $( "#project-id" ).val( ui.item.value );
        $( "#project-description" ).html( ui.item.desc );
        $( "#project-icon" ).attr( "src", "images/" + ui.item.icon );

http://jsfiddle.net/6wTHK/4/

【问题讨论】:

  • 你的小提琴有几个打字错误^^不能工作!
  • 您在JavaScript line 65 的小提琴中缺少{。您还需要包含jQueryjQuery UI
  • 还在您的输入字段中识别出唯一违规行为。他们不能有相同的ID。
  • 对不起那些家伙。好的,现在已经修复了,谢谢,但是我如何让第二个字段成为一个下拉框,具有完全不同的值?
  • 填写第二个 var foo =[{ yikes:'super' }, ..] 并像第一个一样在后面提供所有 jquery 内容。

标签: jquery html autocomplete jquery-autocomplete


【解决方案1】:

所以我做了一个例子来向你解释你需要做些什么来做更多的输入。

FIDDLE:http://jsfiddle.net/6wTHK/6/ 不是如下代码所示

假设我们有两个inputs

<input  name="project1" id="searchbox1" placeholder="Cmpt 1"/>
<input  name="project2" id="searchbox2" placeholder="Cmpt 2"/>

#searchbox1 的值保存在var projects1

var projects1 = [
      {
        value: "CMPT101",
        label: "CMPT 101",
        desc: "Discrete Mathematics I"
},
{
        value: "CMPT102",
        label: "CMPT 102",
        desc: "Discrete Mathematics II"
},
{
        value: "CMPT103",
        label: "CMPT 103",
        desc: "Discrete Mathematics III"
}];

#searchbox2 的值保存在var projects2

var projects2 = [
          {
            value: "CMPT104",
            label: "CMPT 105",
            desc: "Discrete Mathematics IV"
    },
    {
            value: "CMPT106",
            label: "CMPT 106",
            desc: "Discrete Mathematics V"
    },
    {
            value: "CMPT107",
            label: "CMPT 107",
            desc: "Discrete Mathematics VI"
    }];

现在我们为每个input 添加.autocomplete() 函数;

 $( "#searchbox1" ).autocomplete({ //change #searchbox2 to your input id
      minLength: 0,
      source: projects1, //change here the source of your values
      focus: function( event, ui ) {
        $( "#searchbox1" ).val( ui.item.label );
        //you had more stuff here
        return false;
      },
      select: function( event, ui ) {
        $( "#searchbox1" ).val( ui.item.label );
        //you had more stuff here
        return false;
      }, 
      })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li>" )
        .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
        .appendTo( ul );
    };

第二个input

$( "#searchbox2" ).autocomplete({ //change #searchbox2 to your input id
          minLength: 0,
          source: project2, //change here the source of your values
          focus: function( event, ui ) {
            $( "#searchbox2" ).val( ui.item.label );
            //you had more stuff here
            return false;
          },
          select: function( event, ui ) {
            $( "#searchbox2" ).val( ui.item.label );
            //you had more stuff here
            return false;
          }, 
          })
        .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
          return $( "<li>" )
            .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
            .appendTo( ul );
        };

【讨论】:

  • 好的,非常感谢,现在让我试试吧,因为我做过一次,但也许我错过了一些东西
  • 我在这里说我爱你可以吗???非常感谢你,我希望我知道的和你一样多
  • 我认为这就是 stackOVERflow 的意思。也有很多粗鲁的家伙。但有一天他们需要帮助:))
  • 是否可以确定drop box的高度,所以你必须向下滚动才能看到更多
  • @codelio 是否可以确定下拉框的高度,所以您必须向下滚动才能看到更多?
猜你喜欢
  • 1970-01-01
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 2016-07-09
相关资源
最近更新 更多