Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery-在字符串中的任意位置查找模式?_Jquery_Pattern Matching_Selector - Fatal编程技术网

jquery-在字符串中的任意位置查找模式?

jquery-在字符串中的任意位置查找模式?,jquery,pattern-matching,selector,Jquery,Pattern Matching,Selector,当涉及选择器和模式时,我仍然有点困惑。。。基本上,我尝试查找输入项的父div,如果在其ID中的任何位置找到字符串,我希望将其设置为“无” 我以前做过这样的事情,比如: if($('div[id*=string]')) { $(this).attr('display','none'); } 但是,我不知道如何对一个变量这样做 这是我能做的,然后我就被卡住了 $('input.rclass').each(function() { var myDiv = $(this).parent().p

当涉及选择器和模式时,我仍然有点困惑。。。基本上,我尝试查找输入项的父div,如果在其ID中的任何位置找到字符串,我希望将其设置为“无”

我以前做过这样的事情,比如:

if($('div[id*=string]')) { $(this).attr('display','none'); }
但是,我不知道如何对一个变量这样做

这是我能做的,然后我就被卡住了

$('input.rclass').each(function() {
    var myDiv = $(this).parent().parent();
    if($(myDiv...
});
标记如下所示:

<div id="edit-gci" class="form-item">
   <label for="editgci[0][foa][value]" class="option">
     <input type="radio" class="rclass" value="" name="editgci[0][foa][value]" id="editgci-0-value-string-idnum"> N/A
   </label>
</div>

不适用

如果你可以假设它总是

 $('input.rclass').each(function() {
      var myDiv = $(this).parent().parent();
      if(myDiv.attr("id").length >0){
           $(myDiv).hide(); 
      }
 });

忘记检查id属性长度是否大于0

attr用于设置元素的属性,因此您的行:

if($('div[id*=string]')) { $(this).attr('display','none'); }
应该是:

if($('div[id*=string]')) { $(this).css('display','none'); }

改用
css
方法。

在JavaScript中,可以使用正则表达式在另一个字符串中查找字符串

if(/string/.test($(myDiv).attr('id'))) { $(this).hide(); }
您可以使用jQuery.closest()


这并不能回答您的问题,但您应该遵循HTML输入标记的标准并正确关闭它:
N/A
我想您的意思是
$(myDiv).attr('id')
$(myDiv)[0].id
$('input.rclass').cloest('.form-item').hide();