【问题标题】:jQuery filtering and toggling for searchingjQuery 过滤和切换搜索
【发布时间】:2020-12-10 07:59:17
【问题描述】:

我有多个聊天记录作为联系人卡片,并且我有一个用于搜索他们的联系号码的搜索框。对于我的搜索框,我使用 onKeydown 进行搜索,例如 whatsapp 搜索与号码的联系人。 但是我的搜索没有按预期工作,搜索将显示全部或隐藏我的所有聊天记录。

如何搜索号码并显示正确的聊天记录。

<div class="contactlist-container">

    <div id="chatlog" value="0105653762">
        <div class="chatlog-avatar">
            <img src="res/images/user-avatar.png" style="height: 40px; width: 40px;"/>
        </div>
        <div class="chatlog-contact">
            <div id="chatlog-contactnumber">
                <span>
                    010-565 3762
                </span>
            </div>
            <div id="chatlog-lastchat">
                <span>
                    Hello there
                </span>
            </div>
        </div>
    </div>

    <div id="chatlog" value="010777777">
        <div class="chatlog-avatar">
            <img src="res/images/user-avatar.png" style="height: 40px; width: 40px;"/>
        </div>
        <div class="chatlog-contact">
            <div id="chatlog-contactnumber">
                <span>
                    010-777 7777
                </span>
            </div>
            <div id="chatlog-lastchat">
                <span>
                    Hello there, Welcome to our customer service system.
                </span>
            </div>
        </div>
    </div>

    <div id="chatlog" value="010888888">
        <div class="chatlog-avatar">
            <img src="res/images/user-avatar.png" style="height: 40px; width: 40px;"/>
        </div>
        <div class="chatlog-contact">
            <div id="chatlog-contactnumber">
                <span>
                    010-888 8888
                </span>
            </div>
            <div id="chatlog-lastchat">
                <span>
                    Welcome, my name is Kinabalu
                </span>
            </div>
        </div>
    </div>

</div>

jQuery:

$('.search-container input#chatsearch').on("keyup", function(){
    var number = $(this).val().toLowerCase();
    $('#chatlog #chatlog-contactnumber span').filter(function(){
        $('.contactlist-container #chatlog').toggle($('#chatlog-contactnumber span')
            .text().toLowerCase().indexOf(number) >  -1);
    });
    //
    //  Something gonna edit here, cant target current chatlog
    //
});

【问题讨论】:

    标签: jquery filter toggle


    【解决方案1】:

    请试试这个 您在 HTML 中犯了一些错误,请检查以下代码。

    <div class="contactlist-container" id="chatlog_container">
      <div class="chatlog" value="0105653762">
        <div class="chatlog-avatar">
          <img src="res/images/user-avatar.png" style="height: 40px; width: 40px;"/>
        </div>
        <div class="chatlog-contact">
          <div class="chatlog-contactnumber">
            <span>010-565 3762</span>
          </div>
          <div id="chatlog-lastchat">
            <span>Hello there</span>
          </div>
        </div>
      </div>
    
      <div class="chatlog" value="010777777">
        <div class="chatlog-avatar">
          <img src="res/images/user-avatar.png" style="height: 40px; width: 40px;"/>
        </div>
        <div class="chatlog-contact">
          <div class="chatlog-contactnumber">
            <span>010-777 7777</span>
          </div>
          <div id="chatlog-lastchat">
            <span>Hello there, Welcome to our customer service system.</span>
          </div>
        </div>
      </div>
    
      <div class ="chatlog" value="010888888">
        <div class="chatlog-avatar">
          <img src="res/images/user-avatar.png" style="height: 40px; width: 40px;"/>
        </div>
        <div class="chatlog-contact">
          <div class="chatlog-contactnumber">
            <span>010-888 8888</span>
          </div>
          <div id="chatlog-lastchat">
            <span>Welcome, my name is Kinabalu</span>
          </div>
        </div>
      </div>
    </div>
    

    jquery函数

    $('.search-container input#chatsearch').on("keyup", function(){
        var number = $(this).val();
        $('#chatlog_container .chatlog .chatlog-contact .chatlog-contactnumber span').filter(function(){
            $(this).parent().parent().parent().toggle($(this).text().indexOf(number) > -1);
        });
    });
    

    【讨论】:

    • 谢谢它正在工作,但我在 HTML 上的错误是什么?你能解释一下吗?
    • 您多次使用#chatlog id,每页只能使用一个id。该类可以多次使用。请阅读以下文档。 w3schools.com/html/html_id.asp
    • 谢谢,您的 HTML 中的 chatlog-lastchat 需要更改为 class 而不是 id?
    • 是的,请查看我的代码,您会得到更好的主意。
    猜你喜欢
    • 2010-10-24
    • 2020-12-13
    • 2016-12-24
    • 1970-01-01
    • 2017-08-20
    • 2014-10-12
    • 2021-02-11
    • 2013-07-30
    • 1970-01-01
    相关资源
    最近更新 更多