为什么我的jQuery UI自动完成小部件无法工作?

时间:2014-04-05 06:19:58

标签: jquery jquery-ui autocomplete

我有那个意图自动完成两个标签的输入。顺便说一句,附加输入。

附加的输入

htmlsignup += "<input placeholder='Ваш пол' type='text' value='' class='signup_sex_input' name='signup_sex_input' id='signup_sex_input'/><br>";

自动填充部分

$(document).on('keyup',"input#signup_sex_input",function(){
(function() {var tags_for_autocomplete = ["Мужской","Женский"];
$("input[name='signup_sex_input']").autocomplete({
      source: tags_for_autocomplete
});
});
});

你觉得我做错了什么?我将ui库连接到页面。

1 个答案:

答案 0 :(得分:0)

您不需要处理keyup事件 - autocomplete会为您管理所有这些:

尝试用以下方法替换您的功能:

$(function() {
    var tags_for_autocomplete = ["Мужской","Женский"];
    $( "#signup_sex_input" ).autocomplete({
      source: tags_for_autocomplete
    });
  });

Fiddle here

修改

Working fiddle adding to the DOM with jQuery

$(function() {
    var htmlsignup = "<input placeholder='Ваш пол' type='text' value='' class='signup_sex_input' name='signup_sex_input' id='signup_sex_input'/><br>";

    $('body').append(htmlsignup);
    var tags_for_autocomplete = ["Мужской","Женский"];
    $( "input[name='signup_sex_input']" ).autocomplete({
      source: tags_for_autocomplete
    });
  });