Jquery在元素中搜索两组字符串

时间:2016-03-18 20:08:28

标签: jquery string contains

尝试让Jquery针对从两个变量的组合加载的字符串运行搜索。

以下是更多上下文的代码。 此代码的目标是在SharePoint表中搜索通过按下按钮给出的值,以及用户可以选择键入的值。用户可以仅使用搜索输入(txt变量)或按钮进行搜索。

var ButtonValue = null
$("#button1").click(function(){


            $("#WebPartWPQ5").find("tr.ms-itmhover:not(:Contains('Faucets'))").hide();
             $("#WebPartWPQ5").find("tr.ms-itmhover:Contains('Faucets')").show();

             $("#WebPartWPQ6").find("tr.ms-itmhover:not(:Contains('Faucets'))").hide();
             $("#WebPartWPQ6").find("tr.ms-itmhover:Contains('Faucets')").show();

            $("#WebPartWPQ4").find("tr.ms-itmhover:not(:Contains('Faucets'))").hide();
             $("#WebPartWPQ4").find("tr.ms-itmhover:Contains('Faucets')").show();

            $("#WebPartWPQ7").find("tr.ms-itmhover:not(:Contains('Faucets'))").hide();
             $("#WebPartWPQ7").find("tr.ms-itmhover:Contains('Faucets')").show();

            $("#WebPartWPQ2").find("tr.ms-itmhover:not(:Contains('Faucets'))").hide();
             $("#WebPartWPQ2").find("tr.ms-itmhover:Contains('Faucets')").show();

            $("#WebPartWPQ8").find("tr.ms-itmhover:not(:Contains('Faucets'))").hide();
             $("#WebPartWPQ8").find("tr.ms-itmhover:Contains('Faucets')").show()
             ButtonValue = "Faucets" 

             });

$("input.search").change(function() {
              var txt = $("input.search").val();



        if (txt) {
              //alert("VN : " + VN);
              //alert(".change() called");

             //Vendor List
             $("#WebPartWPQ5").find("tr.ms-itmhover:not(:Contains("+txt+" "+ButtonValue+"))").hide();
             $("#WebPartWPQ5").find("tr.ms-itmhover:Contains("+txt+" "+ButtonValue+" )").show();

             //Manufacturer Reps
             // Logic to see search value is numeric then search using the company name
             $("#WebPartWPQ6").find("tr.ms-itmhover:not(:Contains("+txt+" "+ButtonValue+"))").hide();
             $("#WebPartWPQ6").find("tr.ms-itmhover:Contains("+txt+" "+ButtonValue+")").show();

             //Vendor Contact List
             $("#WebPartWPQ4").find("tr.ms-itmhover:not(:Contains("+txt+" "+ButtonValue+"))").hide();
             $("#WebPartWPQ4").find("tr.ms-itmhover:Contains("+txt+" "+ButtonValue+")").show();

             //Vendor Document List
             $("#WebPartWPQ7").find("tr.ms-itmhover:not(:Contains("+txt+" "+ButtonValue+"))").hide();
             $("#WebPartWPQ7").find("tr.ms-itmhover:Contains("+txt+" "+ButtonValue+")").show();

             // Vendor Details
             $("#WebPartWPQ2").find("tr.ms-itmhover:not(:Contains("+txt+" "+ButtonValue+"))").hide();
             $("#WebPartWPQ2").find("tr.ms-itmhover:Contains("+txt+" "+ButtonValue+")").show();

             // Product Lines
             $("#WebPartWPQ8").find("tr.ms-itmhover:not(:Contains("+txt+" "+ButtonValue+"))").hide();
             $("#WebPartWPQ8").find("tr.ms-itmhover:Contains("+txt+ "+ButtonValue+"")").show();

            } else {

              $("#WebPartWPQ6").find("tr.ms-itmhover").show();
              $("#WebPartWPQ7").find("tr.ms-itmhover").show();
              $("#WebPartWPQ4").find("tr.ms-itmhover").show();
              $("#WebPartWPQ8").find("tr.ms-itmhover").show();
              $("#WebPartWPQ2").find("tr.ms-itmhover").show();
              $("#WebPartWPQ5").find("tr.ms-itmhover").show();           
            }
          }).keyup(function(){$(this).change();
         });


$("#button1").click(function() {
      $.ajax('/forward');
      $('button1').removeClass('active');
      $(this).addClass('active');
});

2 个答案:

答案 0 :(得分:0)

你错过了一个' +'在txt变量之前的第一行:

$("#WebPartWPQ5").find("tr.ms-itmhover:not(:Contains("+txt+" "+ButtonValue+"))").hide();
$("#WebPartWPQ5").find("tr.ms-itmhover:Contains("+txt+" "+ButtonValue+"))").show();

答案 1 :(得分:0)

由于您要查找其中一个文本,因此需要定义2个单独的:contains选择器。

$("#WebPartWPQ5").find("tr.ms-itmhover td").hide();
$("#WebPartWPQ5 tr.ms-itmhover td:contains("+txt+"), #WebPartWPQ5 tr.ms-itmhover td:contains("+ButtonValue+")").show();

示例:https://jsfiddle.net/uyk0kopq/2/