简体   繁体   English

JavaScript startsWith和endsWith

[英]JavaScript startsWith and endsWith

I have a simple form with 2 option elements. 我有2个选项元素的简单形式。 When the user hits the "Show Results" button, it will hide the certain table rows based on the month and/or year that was inputted. 当用户单击“显示结果”按钮时,它将根据输入的月份和/或年份隐藏某些表行。 So for example, November and 2014 were the selected options and the button was clicked, it should show rows whose string matches it and hides those that didn't fit the criteria. 因此,例如,11月和2014年是选定的选项,并且单击了按钮,它将显示字符串匹配的行,并隐藏不符合条件的行。 I tried something like putting all td rows with ids of each month but that is very redundant because the list would grow. 我尝试过像将所有td行的每个月的id都放入一样,但这是非常多余的,因为列表会增长。 I also tried startsWith() and endsWith() for month and year, respectively. 我还分别尝试了月份和年份的startsWith()和endsWith()。

        $("figure.col-sm-3").each(function () {
            var getMonth = document.getElementById('table').getElementsByTagName('tr')[3].getElementsByTagName('td').startsWith("November");
            $("figure.col-sm-3").hide;
            $(getMonth).show;
            )
        });

<section class="container">
    <div class="row">
    <!-- FORM AREA -->
        <p>Show items by:</p>
        <form class="form-horizontal">
            <div class="form-group">
            <label for="inputMonth" class="col-sm-2 control-label">Select Month</label>
                <div class="col-sm-10">
                    <select class="form-control" id="inputMonth">
                        <option value="none"> - </option>
                        <option value="January">January</option>
                        <option value="February">February</option>
                        <option value="March">March</option>
                        <option value="April">April</option>
                        <option value="May">May</option>
                        <option value="June">June</option>
                        <option value="July">July</option>
                        <option value="August">August</option>
                        <option value="September">September</option>
                        <option value="October">October</option>
                        <option value="November">November</option>
                        <option value="December">December</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="inputYear" class="col-sm-2 control-label">Select Year</label>
                <div class="col-sm-10">
                    <select class="form-control" id="inputYear">
                        <option value="2014">2014</option>
                        <option value="2015">2015</option>
                        <option value="2016">2016</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button class="btn btn-primary">Show results</button>
                </div>
            </div>
        </form>
    </div>
</section>

<section class="container">
    <div class="row"> <!-- FIRST SET OF ITEMS -->
        <figure class="col-sm-3 thumbnail">
            <h6 class="text-center text-uppercase">Item 1</h6>
            <img src="img1.jpg" />
                <table class="table">
                    <tr class="price">
                        <td>Price</td>
                        <td>250</td>
                    </tr>
                    <tr>
                        <td>Condition</td>
                        <td>new</td>
                    </tr>
                    <tr>
                        <td>Size</td>
                        <td>36</td>
                    </tr>
                    <tr>
                        <td>Sold on</td>
                        <td>November 7, 2014</td>
                    </tr>
                </table>
        </figure>
        <figure class="col-sm-3 thumbnail">
            <h6 class="text-center text-uppercase">Item 2</h6>
            <img src="img2.jpg" />
                <table class="table">
                    <tr class="price">
                        <td>Price</td>
                        <td>250</td>
                    </tr>
                    <tr>
                        <td>Condition</td>
                        <td>new</td>
                    </tr>
                    <tr>
                        <td>Size</td>
                        <td>36</td>
                    </tr>
                    <tr>
                        <td>Sold on</td>
                        <td>December 2, 2014</td>
                    </tr>
                </table>
        </figure>
        <figure class="col-sm-3 thumbnail">
            <h6 class="text-center text-uppercase"Item 3</h6>
            <img src="img3.jpg" />
                <table class="table">
                    <tr class="price">
                        <td>Price</td>
                        <td>250</td>
                    </tr>
                    <tr>
                        <td>Condition</td>
                        <td>new</td>
                    </tr>
                    <tr>
                        <td>Size</td>
                        <td>L</td>
                    </tr>
                    <tr>
                        <td>Sold on</td>
                        <td>February 19, 2015</td>
                    </tr>
                </table>
        </figure>
        <figure class="col-sm-3 thumbnail">
            <h6 class="text-center text-uppercase">Item 4</h6>
            <img src="img4.jpg" />
                <table class="table">
                    <tr class="price">
                        <td>Price</td>
                        <td>250</td>
                    </tr>
                    <tr>
                        <td>Condition</td>
                        <td>new</td>
                    </tr>
                    <tr>
                        <td>Size</td>
                        <td>XL</td>
                    </tr>
                    <tr>
                        <td>Sold on</td>
                        <td>February 19, 2015</td>
                    </tr>
                </table>
        </figure>
    </div>
</section>

You could use this code. 您可以使用此代码。 It does not really do startsWith or endsWith , but I don't see how that is necessary: a date will only have one year in it, and one month. 它并没有真正地执行startsWithendsWith ,但是我不知道这是必要的:一个日期只有一年,一个月。 There is no confusion between the two, so just the mere presence of "November" means it is a match. 两者之间没有混淆,因此仅出现“ 11月”就意味着这是一场比赛。 Requiring that "November" appears at the start seems overkill. 要求“十一月”开始出现似乎是过分的。 The same goes for the year. 今年也是如此。

So, with that in mind, using jQuery's :contains will do it: 因此,考虑到这一点,使用jQuery的:contains可以做到:

$('.btn-primary').click(function() {
    var month = $('#inputMonth').val();
    if (month == 'none') month = '';
    var year = $('#inputYear').val();
    $('div.row').show(); // show all "rows"
    $('figure.col-sm-3')
        .hide() // hide all of them, and then show if both `has` are true
        .has('tr:last-child>td:last-child:contains(' + year + ')')
        .has('tr:last-child>td:last-child:contains(' + month + ')')
        .show();
    // hide any rows that have no more visible figures
    $('div.row:has(figure):not(:has(figure:visible))').hide();
    return false; // to avoid that button submits the form
});

 $('.btn-primary').click(function() { var month = $('#inputMonth').val(); if (month == 'none') month = ''; var year = $('#inputYear').val(); $('div.row').show(); // show all "rows" $('figure.col-sm-3') .hide() // hide all of them, and then show if both `has` are true .has('tr:last-child>td:last-child:contains(' + year + ')') .has('tr:last-child>td:last-child:contains(' + month + ')') .show(); // hide any rows that have no more visible figures $('div.row:has(figure):not(:has(figure:visible))').hide(); return false; // to avoid that button submits the form }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section class="container"> <div class="row"> <!-- FORM AREA --> <p>Show items by:</p> <form class="form-horizontal"> <div class="form-group"> <label for="inputMonth" class="col-sm-2 control-label">Select Month</label> <div class="col-sm-10"> <select class="form-control" id="inputMonth"> <option value="none"> - </option> <option value="January">January</option> <option value="February">February</option> <option value="March">March</option> <option value="April">April</option> <option value="May">May</option> <option value="June">June</option> <option value="July">July</option> <option value="August">August</option> <option value="September">September</option> <option value="October">October</option> <option value="November">November</option> <option value="December">December</option> </select> </div> </div> <div class="form-group"> <label for="inputYear" class="col-sm-2 control-label">Select Year</label> <div class="col-sm-10"> <select class="form-control" id="inputYear"> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button class="btn btn-primary">Show results</button> </div> </div> </form> </div> </section> <section class="container"> <div class="row"> <!-- FIRST SET OF ITEMS --> <figure class="col-sm-3 thumbnail"> <h6 class="text-center text-uppercase">Item 1</h6> <img src="img1.jpg" /> <table class="table"> <tr class="price"> <td>Price</td> <td>250</td> </tr> <tr> <td>Condition</td> <td>new</td> </tr> <tr> <td>Size</td> <td>36</td> </tr> <tr> <td>Sold on</td> <td>November 7, 2014</td> </tr> </table> </figure> <figure class="col-sm-3 thumbnail"> <h6 class="text-center text-uppercase">Item 2</h6> <img src="img2.jpg" /> <table class="table"> <tr class="price"> <td>Price</td> <td>250</td> </tr> <tr> <td>Condition</td> <td>new</td> </tr> <tr> <td>Size</td> <td>36</td> </tr> <tr> <td>Sold on</td> <td>December 2, 2014</td> </tr> </table> </figure> <figure class="col-sm-3 thumbnail"> <h6 class="text-center text-uppercase"Item 3</h6> <img src="img3.jpg" /> <table class="table"> <tr class="price"> <td>Price</td> <td>250</td> </tr> <tr> <td>Condition</td> <td>new</td> </tr> <tr> <td>Size</td> <td>L</td> </tr> <tr> <td>Sold on</td> <td>February 19, 2015</td> </tr> </table> </figure> <figure class="col-sm-3 thumbnail"> <h6 class="text-center text-uppercase">Item 4</h6> <img src="img4.jpg" /> <table class="table"> <tr class="price"> <td>Price</td> <td>250</td> </tr> <tr> <td>Condition</td> <td>new</td> </tr> <tr> <td>Size</td> <td>XL</td> </tr> <tr> <td>Sold on</td> <td>February 19, 2015</td> </tr> </table> </figure> </div> </section> 

$("figure.col-sm-3").each(function () {
  var $months = $('table tr:nth-child(3)')
                .find('td:not(:contains("November"))')
                .closest('tr').hide();
 });

Hope that helps! 希望有帮助!

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

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