简体   繁体   English

单击按钮返回所有链接

[英]Return all links on button click

I have a page that contains a large amount of links. 我的页面包含大量链接。

I have integrated a search box for the user to type in the link name, and when they hit the button 'Search', it returns all links that contain what the user typed in: 我已经集成了一个搜索框供用户键入链接名称,当他们点击“搜索”按钮时,它将返回包含用户键入内容的所有链接:

Here is a section of HTML for this page: 这是此页面的HTML部分:


<h4><a href="#" id="Trig16">First Header</a></h4>
<div class="slider" id="Slide16">
    <ul>
        <li><a href="/TestDocument.doc" target="_blank">Document 1</a></li>
        <li><a href="/TestDocument2.doc" target="_blank">Document 2</a></li>
        <li><a href="/TestDocument3.doc" target="_blank">Document 3</a></li>
    </ul>
</div>

<h4><a href="#" id="Trig19">Second Header</a></h4>
<div class="slider" id="Slide19">
    <ul>
        <li><a href="~/Docs/Document4.pdf" target="_blank">Document 4</a></li>
        <li><a href="~/Docs/Document5.pdf" target="_blank">Document 5</a></li>
    </ul>
</div>

Here is the JavaScript for that button: 这是该按钮的JavaScript


$("#ButtonSearch").click(function () {
    var textboxValue = $("#SearchLink").val().toLowerCase();
    $('.slider').each(function () {
        var exist = false;
        $(this).find('ul li').each(function () {
            if ($(this).find('a').text().toLowerCase().indexOf(textboxValue) !== -1) {
                $(this).show();
                exist = true;
            }
            else
                $(this).hide();
        });
        if (exist === false) {
            $(this).prev('h4').hide();
            $(this).hide();
        }
        else {
            $(this).prev('h4').show();
        }
    });
});

Now I also have a clear button.. and when the user hits that button I want all sliders, headers, ul's, li's, and links to return without a page refresh. 现在,我还有一个清除按钮。当用户单击该按钮时,我希望所有滑块,标题,ul,li和链接返回时都不会刷新页面。

Here is what I have so far for that button: 到目前为止,这是该按钮的功能:


$("#ButtonClearSearch").click(function () {
    $("#SearchLink").val("");
    $('.slider').each(function() {
        $(this).prev('h4').show();
    });
});

So just as an example.. if I were the user and typed in Document 1 into the search box.. and hit search, it would return the header First Header and underneath would be the Document 1 . 因此,仅作为示例..如果我是用户,并在搜索框中输入Document 1然后点击搜索,它将返回标头First Header而其下方将是Document 1

Now if I hit Clear search.. it would return me all of my h4 elements, but nothing underneath them, except for the First Header element with the Document 1 underneath.. So it is keeping the original search and just returning the rest of the h4 elements.. 现在,如果我单击“清除搜索”,它将返回我所有的h4元素,但它们下面没有任何东西,除了带有“ Document 1的“ First Header元素。.因此,它将保留原始搜索并仅返回其余的h4元素..

So my question is how do I return everything on click of the clear search button? 所以我的问题是,单击清除搜索按钮后如何返回所有内容?

This will filter in realtime on keypress or paste. 这将在按键或粘贴时实时过滤。 As your user types things into the search if the link contains the text it will hide links that do not contain the text and it will show links that do. 当您的用户在搜索中键入内容时,如果链接包含文本,它将隐藏不包含文本的链接,并显示包含文本的链接。 As the value changes it will show or hide based on the value of the search. 值更改时,它将根据搜索的值显示或隐藏。

Edit: Search is now bound to buttton 编辑:搜索现在绑定到对接

 let filter = function(){ $filter = $('#search-filter').val(); $.each($('a'),function(k,v){ let heading = '#'+$(v).closest('div').attr('data-bind'); let sibs = $(v).parent().siblings(); if(v.innerText.toLowerCase().indexOf( $filter ) > -1){ //Show $(v).closest('li').show(); } else { //Hide $(v).closest('li').hide(); } }); } $('#searchBtn').on('click',filter); $('#clrBtn').on('click',function(){ $("#search-filter").val(""); filter(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="search-filter"> <input id="searchBtn" type="button" value="Search"> <input id="clrBtn" type="button" value="clear"> <h4 id="Trig16">First Header</h4> <div class="slider" id="Slide16" data-bind="Trig16"> <ul> <li><a href="/TestDocument.doc" target="_blank">Document 1</a></li> <li><a href="/TestDocument2.doc" target="_blank">Document 2</a></li> <li><a href="/TestDocument3.doc" target="_blank">Document 3</a></li> </ul> </div> <h4 id="Trig19">Second Header</h4> <div class="slider" id="Slide19" data-bind="Trig19"> <ul> <li><a href="~/Docs/Document4.pdf" target="_blank">Document 4</a></li> <li><a href="~/Docs/Document5.pdf" target="_blank">Document 5</a></li> </ul> </div> 

I have figured this out, using what I already have: 我已经使用现有的方法解决了这个问题:

$("#ButtonClearSearch").click(function () {
    $("#SearchLink").val("");
    $('.slider').each(function() {
        $(this).prev('h4').show();
        $(this).children().each(function () {
           $('ul li').show();
           $('a').show();
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM