Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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_Input_Jquery Selectors - Fatal编程技术网

输入值的jquery选择器

输入值的jquery选择器,jquery,input,jquery-selectors,Jquery,Input,Jquery Selectors,我这里有密码 <div> <input type="hidden" value="hello" /> </div> <div> <input type="hidden" value="world" /> </div> 请帮忙 您要选择输入,然后获取其父级div: $("input[value='hello']").parent("div").css("background", "red"); 这

我这里有密码

 <div>
    <input type="hidden" value="hello" />
 </div>

 <div>
    <input type="hidden" value="world" />
 </div>

请帮忙

您要选择输入,然后获取其父级
div

$("input[value='hello']").parent("div").css("background", "red");
这就是:

$("div > input[value=hello]").parent().css("color", "red");

或者,如果“颜色”的真正含义是“背景色”:


为了提供信息,将此信息发布出去

在实践中,我会使用或解决方案

$("div:has( > input[value='hello'] )").css("background", "red");
这将使用选择具有作为直接后代的
元素


我更喜欢其他的原因是因为它们使用了非常简单有效的CSS选择器。这是一个有效的替代方案,但可能会执行得稍慢一些。

对于遇到此问题的人来说,在搜索特定的
值作为属性时,这些答案是正确的,即在HTML中硬编码的值-根据所问问题完全正确。但是,如果用户在任何时候更改了字段的值,则不会更新value属性,只更新元素的value属性。这将导致以选择实际具有不同当前值的元素的形式出现意外行为。。。相反,或者至少在我找到更好的方法之前,我一直在使用以下方法:

jQuery.extend(
  jQuery.expr[':'],
  {
    /// check that a field's value property has a particular value
    'field-value': function (el, indx, args) {
      var a, v = $(el).val();
      if ( (a = args[3]) ) {
        switch ( a.charAt(0) ) {
          /// begins with
          case '^':
            return v.substring(0,a.length-1) == a.substring(1,a.length);
          break;
          /// ends with
          case '$':
            return v.substr(v.length-a.length-1,v.length) == 
              a.substring(1,a.length);
          break;
          /// contains
          case '*': return v.indexOf(a.substring(1,a.length)) != -1; break;
          /// equals
          case '=': return v == a.substring(1,a.length); break;
          /// not equals
          case '!': return v != a.substring(1,a.length); break;
          /// equals
          default: return v == a; break;
        }
      }
      else {
        return !!v;
      }
    }
  }
);
上面创建了一个新的jQuery伪选择器,可以这样使用:

$('input:field-value(^test)');
将选择以值“test”开头的所有输入,或:

它将选择在其值的任何位置包含“test”的所有输入


还支持
不是,
$
以或
=
等于…

以上答案@pebbl的咖啡脚本版本结尾

##############################################################################
###
    jQuery select extend 
    @pebbl http://stackoverflow.com/a/15031698/1271868
##############################################################################
jQuery ->
  jQuery.extend(
    jQuery.expr[':'],
    # check that a field's value property has a particular value
    'field-value': (el, indx, args) ->
      v = $(el).val()
      if (a = args[3])
        switch a.charAt(0)
          # begins with
          when '^' then return v.substring(0, a.length-1) is
            a.substring(1, a.length)
          # ends with
          when '$' then return v.substr(v.length-a.length-1, v.length) is 
            a.substring(1, a.length)
          # contains
          when '*' then return v.indexOf(a.substring(1, a.length)) isnt -1 
          # equals
          when '=' then return v is a.substring(1, a.length) 
          # not equals
          when '!' then return v isnt a.substring(1, a.length) 
          # equals
          else return v is a 
      else
        return !!v
  )

@Phil:BoltClock的解决方案找到任何值为
hello
input
,然后只选择
div
元素的父元素,并将其背景设置为红色
input
具有不同值的元素将被忽略,并且具有值
hello
且其父元素不是
div
元素的输入元素也不会得到更新(尽管方式不同)。不过,它的效率不如预期的高。@vrynxzent:要从浏览器可能具有的任何内置选择器处理中获取最大值,请将第一部分更改为
$(“div>输入[value='hello'])
。这将剔除不是BoltClock稍后的
.parent(“div”)
元素的直接子元素的输入,但是在一个无法优化的层上。根据问题,这是100%正确的答案,但可能值得注意的是,对于用户可以更改的选择或文本输入,这实际上不起作用。下面的Pebbl的解决方案解决了这个问题。@Jan Zyka:事实上,我还没有看到Pebbl的答案,所以谢谢你的提醒。这个问题有点含糊不清,因为它没有指定想要的是针对属性还是属性。除非您知道差异,并且知道您希望该属性,否则此解决方案可能看起来有效,但可能会以您不期望的方式运行,正如其他人所指出的。非常感谢您的解释和解决方案,只是为了“验证”您的理论,另一种可能的解决方案是向(全部或特定)添加事件像这样的输入可以强制创建value属性:
$(document).on(“更改”,“输入”,function(){$(this.attr)(“值”,“$(this.val());})现在,默认提供的选择器
输入[value=test]
也可以“预期”工作。尽管如此,还是为您的解释和解决方案竖起大拇指!
$('input:field-value(^test)');
$('input:field-value(*test)');
##############################################################################
###
    jQuery select extend 
    @pebbl http://stackoverflow.com/a/15031698/1271868
##############################################################################
jQuery ->
  jQuery.extend(
    jQuery.expr[':'],
    # check that a field's value property has a particular value
    'field-value': (el, indx, args) ->
      v = $(el).val()
      if (a = args[3])
        switch a.charAt(0)
          # begins with
          when '^' then return v.substring(0, a.length-1) is
            a.substring(1, a.length)
          # ends with
          when '$' then return v.substr(v.length-a.length-1, v.length) is 
            a.substring(1, a.length)
          # contains
          when '*' then return v.indexOf(a.substring(1, a.length)) isnt -1 
          # equals
          when '=' then return v is a.substring(1, a.length) 
          # not equals
          when '!' then return v isnt a.substring(1, a.length) 
          # equals
          else return v is a 
      else
        return !!v
  )