将我的自动完成功能绑定到我的添加输入功能

发布于 2024-11-16 04:21:48 字数 1343 浏览 0 评论 0 原文

我一直在尝试将两个函数绑定在一起,但没有成功:我希望在用户单击“添加”时添加的输入字段上运行自动完成功能。我可以让这两个函数单独运行,并发现我应该绑定这两个函数......但作为一个菜鸟,我几乎确定我做错了。任何帮助表示赞赏!

这是自动完成代码:

 var J = jQuery.noConflict();
 J(document).ready(function(){
 J('#professor').autocomplete({source:'autocomplete.php', minLength:1});
 });

这是添加输入代码:

    var J = jQuery.noConflict();
J(document).ready(function(){
J("#addproffield").click(function(){
var input = J("<div class='added-field'><input type='text' name='professor[]' id='professor' size='25' value='' />&nbsp;&nbsp;<a href='' class='remove-btn'>Remove field</a></div>");
J("#addprof").append(input);
});
J(".remove-btn").live('click',function() {
J(this).parent().remove();
});
});

这是我绑定两者的失败尝试:

   var J = jQuery.noConflict();
J(document).ready(function(){
J("#addproffield").click(function(){
var input = J("<div class='added-field'><input type='text' name='professor[]' id='professor' size='25' value='' />&nbsp;&nbsp;<a href='' class='remove-btn'>Remove field</a></div>");
J("#addprof").append(input);
input.professor({
     source:'autocomplete.php', minLength:1
    });
});
J(".remove-btn").live('click',function() {
J(this).parent().remove();
});
    });

谢谢!!

I've been trying to bind two functions together without success: I want an autocomplete to run on input fields that are added when the user clicks on "add". I can get the two to run separately and have figured out that I should be binding the two functions...but as a noob, I'm almost sure I'm doing it wrong. Any help is appreciated!!

here is the autocomplete code:

 var J = jQuery.noConflict();
 J(document).ready(function(){
 J('#professor').autocomplete({source:'autocomplete.php', minLength:1});
 });

Here is the add input code:

    var J = jQuery.noConflict();
J(document).ready(function(){
J("#addproffield").click(function(){
var input = J("<div class='added-field'><input type='text' name='professor[]' id='professor' size='25' value='' />  <a href='' class='remove-btn'>Remove field</a></div>");
J("#addprof").append(input);
});
J(".remove-btn").live('click',function() {
J(this).parent().remove();
});
});

here is my failed attempt to bind the two:

   var J = jQuery.noConflict();
J(document).ready(function(){
J("#addproffield").click(function(){
var input = J("<div class='added-field'><input type='text' name='professor[]' id='professor' size='25' value='' />  <a href='' class='remove-btn'>Remove field</a></div>");
J("#addprof").append(input);
input.professor({
     source:'autocomplete.php', minLength:1
    });
});
J(".remove-btn").live('click',function() {
J(this).parent().remove();
});
    });

Thank you!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我最亲爱的 2024-11-23 04:21:48

您在尝试中将 autocmplete (.professor?) 绑定到

而不是 ,因此它只需要稍加调整即可通过 元素rel="nofollow">.find(),如下所示:

J(document).ready(function(){
    J("#addproffield").click(function(){
    var div = J("<div class='added-field'><input type='text' name='professor[]' size='25' value='' />  <a href='' class='remove-btn'>Remove field</a></div>");
    J("#addprof").append(div);

    div.find("input").autocomplete({
        source:'autocomplete.php', minLength:1
    });
});

注意上面的 html 更改,id 现在不需要也不应该存在,因为多次使用同一个 ID 是无效的。

You're binding the autocmplete (.professor?) to the <div> instead of the <input> in your attempt, so it just needs a little adjustment to actually be the <input> element via .find(), like this:

J(document).ready(function(){
    J("#addproffield").click(function(){
    var div = J("<div class='added-field'><input type='text' name='professor[]' size='25' value='' />  <a href='' class='remove-btn'>Remove field</a></div>");
    J("#addprof").append(div);

    div.find("input").autocomplete({
        source:'autocomplete.php', minLength:1
    });
});

Note the html changes above, the id need not be there now and shouldn't be, since it's invalid to use the same ID multiple times.

瑕疵 2024-11-23 04:21:48

您已经很接近了,请在上面的代码中尝试使用 input.find("#professor").autocomplete,而不是 input.professor

find 方法基本上是在元素内搜索选择器(在本例中为 id)。

You're close, instead of input.professor, try input.find("#professor").autocomplete in your code above.

The find method basically searches for a selector (id in this case) within the element.

空‖城人不在 2024-11-23 04:21:48

jQuery 中的 live 函数非常适合在 DOM 更改时动态地将特定事件处理程序应用于选定元素,但(据我所知)没有一种方法可以在选定元素上执行/调用函数其中设置事件处理程序(如本问题中的autocomplete函数)...

因此,您需要执行如下操作:

var J = jQuery.noConflict();

J(document).ready(function(){
    J("#addproffield").click(function(){
        var input = J("<div class='added-field'><input type='text' name='professor[]' size='25' value='' />  <a href='' class='remove-btn'>Remove field</a></div>");
        J("#addprof").append(input);

        //Next line is inefficient and requires the extra class attribute
        //J(".professor").unbind().autocomplete({source:'autocomplete.php', minLength:1});

        //This is better:
        input.find("input").autocomplete({source:'autocomplete.php', minLength:1});
    });

    J(".remove-btn").live('click',function() {
        J(this).parent().remove();
    });
});

我添加了类< code>教授给每个input 元素,因为您不能拥有具有重复 ID 的有效 HTML(并且 jQuery 不处理重复 ID)。

我还更改了 input.professor... 行,以便使用 class="professor 从所有 input 中删除事件(onclick、mouseover 等) " 然后自动完成功能将(再次)应用于所有选定的元素。

您也许可以执行以下操作,但我没有尝试过:

var input = J("<div class='added-field'><input type='text' name='professor[]' id='professor' class='professor' size='25' value='' />  <a href='' class='remove-btn'>Remove field</a></div>").autocomplete({source:'autocomplete.php', minLength:1});
J("#addprof").append(input);

编辑:采纳了尼克的建议/答案并更新了上面的代码,并删除了idclass< /代码> 属性

The live function in jQuery works great for applying specific event handlers to selected elements dynamically as the DOM changes, but there isn't (as far as I know) a method for executing/calling functions on selected elements which set up event handlers (like the autocomplete function in this question)...

So, you'll need to do something like this:

var J = jQuery.noConflict();

J(document).ready(function(){
    J("#addproffield").click(function(){
        var input = J("<div class='added-field'><input type='text' name='professor[]' size='25' value='' />  <a href='' class='remove-btn'>Remove field</a></div>");
        J("#addprof").append(input);

        //Next line is inefficient and requires the extra class attribute
        //J(".professor").unbind().autocomplete({source:'autocomplete.php', minLength:1});

        //This is better:
        input.find("input").autocomplete({source:'autocomplete.php', minLength:1});
    });

    J(".remove-btn").live('click',function() {
        J(this).parent().remove();
    });
});

I've added the class professor to each of the input elements because you can't have valid HTML with duplicated IDs (and jQuery doesn't handle duplicated IDs).

I've also changed the input.professor... line so that events (onclick, mouseover etc) are removed from all inputs with class="professor" and then the autocomplete function is applied (again) to all of the elements selected.

You might be able to do the following but I haven't tried:

var input = J("<div class='added-field'><input type='text' name='professor[]' id='professor' class='professor' size='25' value='' />  <a href='' class='remove-btn'>Remove field</a></div>").autocomplete({source:'autocomplete.php', minLength:1});
J("#addprof").append(input);

EDIT: took Nick's advice/answer and updated code above and removed the id and class attributes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文