简体   繁体   English

jQuery。 找到所有 <tr> 包含所选文字 <option>

[英]Jquery. Find all <tr> containing text from selected <option>

I try to show all lines contaning selected text from option after click on button, this is my code: 单击按钮后,我尝试显示所有包含选项中所选文本的行,这是我的代码:

<select>
 <option>text1</option>
 <option>text2</option>
 <option>text3</option>
 <option>text4</option>
</select>
<button class="show"></button>
<button class="hide"></button>
<table>
 <tr>
  <td>text1</td><td>....</td>
 </tr>
 <tr>
  <td>text2</td><td>....</td>
 </tr>
 <tr>
  <td>text3</td><td>....</td>
 </tr>
 <tr>
  <td>text1</td><td>....</td>
 </tr>
</table>

I try to do something like this but it doesnt work: 我试图做这样的事情,但它不起作用:

$(function(){
  b = $("tr");
  $(".show").on("click", function(){
   var a = $("select option:selected").text();
   $(b).hide();
   if ($("tr:contains('"+a+"')").length) 
    $(this).closest(tr).show();
 });

 $(".hide").on("click", function(){
  $(b).show();              
 });    
});

Can someone help me, pls :) 有人可以帮我吗,:)

You need something like this. 您需要这样的东西。 Don't pollute global space and use proper selectors. 不要污染全局空间并使用适当的选择器。 And there is no need to wrap a jQuery object again. 并且无需再次包装jQuery对象。

$(function() {
    var b = $("table");
    $(".show").on("click", function() {
        var a = $("select option:selected").text();
        b.find("tr").hide().end().find("td:contains('" + a + "')").parent().show();
    });
    $(".hide").on("click", function() {
        b.find("tr").show();
    });
});

Try this : You can use each to check each tr for selected option text and make it visible. 试试看:您可以使用each来检查每个tr的选定选项文本并使其可见。 No need to use closest('tr') as $(this) itself is a TR . $(this)本身是TR无需使用closest('tr')

$(function(){
  b = $("tr");
  $(".show").on("click", function(){
   var a = $("select option:selected").text();
   b.hide();
   //if ($("tr:contains('"+a+"')").length) 
   // $(this).closest(tr).show();
   b.each(function(){
     if($(this).text().indexOf(a)!=-1)
     {
       $(this).show();
     }
  });
 });

 $(".hide").on("click", function(){
  b.show();              
 });    
});

You can't use contains cause match any element that simple contains test(Select all elements that contain the specified text). 您不能使用contains原因匹配任何简单包含测试的元素(选择所有包含指定文本的元素)。 Bu you can use each and match any td with same text and show parent( tr ) like: 但是您可以使用each td并用相同的文本匹配任何td并显示parent( tr ),例如:

 b = $("tr"); $(".show").on("click", function() { var a = $("select option:selected").text(); $(b).hide(); $("td").each(function() { if ($(this).text() == a) { $(this).parents("tr").show(); } }); }); $(".hide").on("click", function() { $(b).show(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select> <option>text1</option> <option>text2</option> <option>text3</option> <option>text4</option> </select> <button class="show">show</button> <button class="hide">hide</button> <table> <tr> <td>text1</td> <td>....</td> </tr> <tr> <td>text2</td> <td>....</td> </tr> <tr> <td>text3</td> <td>....</td> </tr> <tr> <td>text1</td> <td>....</td> </tr> </table> 

Make your buttons run functions directly here. 让您的按钮直接在此处运行功能。

function show() {
   var needle = $("select option:selected").text();
   $('#myTable td').each(function() {
        if ($(this).text() === needle) $(this).show();
   });
}
function hide() {
   var needle = $("select option:selected").text();
   $('#myTable td').each(function() {
        if ($(this).text() === needle) $(this).hide();
   });
}

Take a look at this (jsFiddle) . 看看这个(jsFiddle)

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

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