简体   繁体   English

JS问题与下拉菜单和状态选择

[英]JS issue with dropdown menu and state selection

Here's the fiddle: https://jsfiddle.net/6aevjxkx/38/ 这是小提琴: https : //jsfiddle.net/6aevjxkx/38/

In the dropdown menu, if you select Nebraska, you'll see a school location from Washington state, which shouldn't appear. 在下拉菜单中,如果您选择内布拉斯加州,您会看到华盛顿州的学校位置,该位置不应出现。

The issue I believe is that the script is searching the ENTIRE td section for any matching state initial. 我相信的问题是,脚本正在ENTIRE td部分中搜索任何匹配的状态初始值。 In the case of the school from Washington, it has "NE" in the address, causing it to appear when you choose Nebraska from the dropdown. 对于华盛顿的学校,地址中带有“ NE”,当您从下拉列表中选择内布拉斯加州时,它会出现。

With that said, how can the script be updated so it can only search for a matching state within the div that has a class "state"? 话虽如此,如何更新脚本,使其只能在具有“状态”类的div中搜索匹配状态? Or if there's another solution, my ears are wide open. 或者,如果还有其他解决方案,我的耳朵会睁大。

Thanks ahead! 谢谢你!

Code of the script 脚本代码

$('select#stateCollege').change( function(e) { 
   var letter = $(this).val();
     if (letter === 'ALL') {
         $ ('table.event tr').css('display','inline-block');
     } else {
         $('table.event tr').each( function(rowIdx,tr) {
             $(this).hide().find('td').each( function(idx, td) {
                 if( idx === 0 || idx === 1) {
                     var check = $(this).text();
                     if (check && check.indexOf(letter) > 0) {
                        $(this).parent().css('display','inline-block');
                    } 
                 }
       });             
    });
   }             
});

Something like the following works: 类似于以下作品:

$('table.event tr').each(function(rowIdx, tr) {
    var row = $(this).hide();
    var state = row.find('.state').text().trim();
    if (state === letter)
        row.css('display', 'inline-block');
});

Updated demo: https://jsfiddle.net/6aevjxkx/42/ 更新的演示: https : //jsfiddle.net/6aevjxkx/42/

That is, hide the current row, then find the .text() of its .state element, .trim() that, then rather than .indexOf() just use === to compare to the value in the rather confusingly named letter variable. 也就是说,隐藏当前行,然后找到其.state元素.trim().text() ,而不是.indexOf()仅使用===来与该名称相当混乱的letter变量。

Note that there was no point to using .each() on your td elements given that there is only one td in each row. 注意,由于每行只有一个td ,因此在td元素上使用.each()没有意义。

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

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