0

安德鲁对我的评论的回答引发了这个问题。

根据他在上面链接中的精彩回答,问题底部的代码仅适用于 ONE 小部件。但它是杀手级的好代码,而且很有意义......我想我想要两全其美。很好的 JS,(如果可能的话)并且show()只有我们当时使用的元素才能得到零结果。

如我所见,此代码段是我的问题的主要症结:

        来源:功能(请求,响应){
        jQuery.ajax({
            url: "/autocomplete.json",
            数据: {
            术语:request.term
            },
            成功:函数(数据){
            如果(数据长度 == 0){
        jQuery('span.guest_investor_email').show();
                jQuery('span.investor_field_delete_button').show();
            }
            响应(数据);
            }
        });

目前:

我的页面上有一个按钮,上面写着“添加更多信息”,每次单击它时,都会出现一个自动完成文本字段的新实例,其中包含一些隐藏字段和一个display:none;on guest_investor_email。如果我使用自动完成文本字段,比如说 3 次,我在页面上有 3 个自动完成实例,第三个找到 0 个结果:代码将显示()guest_investor_email文本字段的所有 3 个实例,而不是this一个空白.

问题:

我怎样才能得到类似 jQuery(this).siblings(('span.guest_investor_email').show();工作的东西?

this是一个对象,而不是要选择的元素数组。如果不是this我不介意,只要我知道如何去做。谢谢。


完整代码:

     jQuery(".auto_search_complete").live("点击", function() {
     jQuery(this).autocomplete({
        最小长度:3,

        来源:功能(请求,响应){
        jQuery.ajax({
            url: "/autocomplete.json",
            数据: {
            术语:request.term
            },
            成功:函数(数据){
            如果(数据长度 == 0){
        jQuery('span.guest_investor_email').show();
                jQuery('span.investor_field_delete_button').show();
            }
            响应(数据);
            }
        });
        },

        焦点:函数(事件,用户界面){
        jQuery(this).val(ui.item.user ? ui.item.user.name : ui.item.pitch.name);
        返回假;
        },
        选择:函数(事件,用户界面){
        jQuery(this).val(ui.item.user ? ui.item.user.name : ui.item.pitch.name);
        jQuery(this).siblings('div.hidden_​​fields').children('.poly_id').val(ui.item.user ? ui.item.user.id : ui.item.pitch.id);
        jQuery(this).siblings('div.hidden_​​fields').children('.poly_type').val(ui.item.user ? "User" : "Pitch");
        jQuery(this).siblings('span.guest_investor_email').hide();
                jQuery(this).siblings('span.investor_field_delete_button').show();
                jQuery(this).attr('readonly','readonly');
                jQuery(this).attr('id', "investor-selected");

        返回假;
        }
    }).每个(函数(){
        jQuery(this).data("自动完成")._renderItem = function(ul, item) {
        返回 jQuery("
  • ") .data("item.autocomplete", item) 。附加(”

    " + (item.user ? item.user.name : item.pitch.name) + "
    " + (item.user ? item.user.investor_type : item.pitch.investor_type) + " - " + (item.user ? item.user.city : item.pitch.city) + "

    ") .appendTo(ul); }; }); });
    4

    1 回答 1

    1

    我不完全理解您想要实现的目标,但我认为您想获得绑定.live事件的元素,对吗?

    jQuery(".auto_search_complete").live("click", function() {
        var $this = $(this);
        jQuery(this).autocomplete({
            minLength: 3,
            ...
            success: function (data) {
            if (data.length == 0) {
        jQuery('span.guest_investor_email').show();
                $this.siblings('span.investor_field_delete_button').show();
            }
        ...
    
    于 2012-05-30T14:47:07.570 回答