jQuery:如何克隆自动完成字段? [英] JQuery: How to clone autocomplete fields?

查看:54
本文介绍了jQuery:如何克隆自动完成字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JörnZaefferer的jquery自动完成插件,并且在克隆自动完成字段时似乎无法弄清楚如何使其工作.几乎可以用,因为当我键入文本时,克隆的自动完成字段会显示选择,但我不能选择项目.起初我以为这是浏览器兼容性问题,但是在FF3和Safari中都发生过,所以我猜我错过了一个陷阱.

I am using Jörn Zaefferer's jquery autocomplete plugin, and I can't seem to figure out how to make it work when I clone an autocomplete field. It almost works, in that the cloned autocomplete field displays the choices when the I type in text, but I cannot select items. At first I thought it was a browser-compatibility issue, but it happens in both FF3 and Safari, so I'm guessing there's a gotcha I've missed.

以下是我正在做的工作示例:

Here is a working example of what I'm doing:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Autocomplete Clone Demo</title>
<style>
body {
margin: 40px;
}
.hide_element {
display: none;
}
</style>
<link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"></script>
<script type="text/javascript">
    function setAutocomplete()
    {
        var users = [
          { name: "Fred", id: "1" },
          { name: "Barney", id: "2" },
          { name: "Wilma", id: "3" }
        ];

        $(".user_selector").autocomplete(users, 
            {
                mustMatch: true,
                matchContains: true,
                minChars: 2,
                formatResult: function(row) { return row.name; },
                formatItem: function(row, i, max) { return row.name; }
            }
        );
    }

    var current= 0;

    var addParticipantFields = function() 
    {
        current++;
        $newParticipant = $("#template").clone(true);
        $newParticipant.removeAttr("id");
        $newParticipant.removeClass("hide_element");
        $prefix = "extra" + current;
        $newParticipant.children("div").children(":input").each(function(i) {
            var $currentElem= $(this);
            $currentElem.attr("name",$prefix+$currentElem.attr("name"));
        });
        $newParticipant.appendTo("#participantsField");
        setAutocomplete();
    }

    $(document).ready(function() {
        setAutocomplete();
        $("#addParticipant").live("click", addParticipantFields);

    });
</script>

</head>
<body>
<h1>Test Autocomplete Cloning</h1>
<form id="demo" method="post" action="">
<fieldset id="participantsField">
<label>Participants</label>
<div class="participant">
    <input class="user_selector" name="user" size="30"/>
</div>
</fieldset>

<!-- This is the template for adding extra participants -->
<div class="participant hide_element" id="template">
    <input class="user_selector" name="_user" size="30"/>
</div>

<p><input type="button" id="addParticipant" value="Add Another Participant"></p>
<p><input class="button" type="submit" value="Submit"/></p>
</form>
</body>
</html>

推荐答案

制作

$newParticipant = $("#template").clone(true);

像这样

$newParticipant = $("#template").clone();

当您未在#template上克隆事件时,您的示例在FF中对我有用.

Your example works for me in FF when you don't clone events on #template.

这篇关于jQuery:如何克隆自动完成字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆