jquery 输入值选择器

发布于 2024-10-18 10:53:56 字数 441 浏览 1 评论 0原文

我这里有代码..

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

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

是否可以选择内部值为“hello”的div并将所选div的颜色更改为红色...?

 $("div input[value='hello']").css("background","red"); //i have this in mind
                                                        //but i think its wrong:D

请帮忙..

i have here the code..

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

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

is it possible to select the div with the value "hello" inside and change the selected div's color to red...?

 $("div input[value='hello']").css("background","red"); //i have this in mind
                                                        //but i think its wrong:D

any help please..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌路黄昏 2024-10-25 10:53:56

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

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

You want to select the input, then take its parent div:

$("input[value='hello']").parent("div").css("background", "red");
骄兵必败 2024-10-25 10:53:56

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

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 '

上面创建了一个新的 jQuery 伪选择器,可以这样使用:

$('input:field-value(^test)');

它将选择以值“test”开头的所有输入,或者:

$('input:field-value(*test)');

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

还支持 ! 不、$ 结尾或 = 等于...

: 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 伪选择器,可以这样使用:

它将选择以值“test”开头的所有输入,或者:

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

还支持 ! 不、$ 结尾或 = 等于...

Just as a future note to those that come across this question, these answers are correct for when searching for the specific value as an attribute i.e. the one hard-coded in the HTML — completely correct as per the question asked. However, if the value of the field is changed by the user at any point the value attribute is not updated, only the element's value property. This will lead to unexpected behaviour in the form of selecting elements that actually have different current values... instead — or at least until I find a better way — I've been using the following:

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 '

The above creates a new jQuery pseudo selector, which can be used like so:

$('input:field-value(^test)');

Which will select all inputs that start with the value "test", or:

$('input:field-value(*test)');

Which will select all inputs that contains "test" anywhere in it's value.

Also supported are ! not, $ ends with or = equals...

: 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; } } } );

The above creates a new jQuery pseudo selector, which can be used like so:

Which will select all inputs that start with the value "test", or:

Which will select all inputs that contains "test" anywhere in it's value.

Also supported are ! not, $ ends with or = equals...

霊感 2024-10-25 10:53:56

这样做:

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

实例

或者,如果“颜色”实际上是指“背景颜色”:

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

实例

This does it:

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

Live example

Or if by "color" you really meant "background color":

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

Live example

记忆里有你的影子 2024-10-25 10:53:56

出于提供信息的目的将其扔在那里。

在实践中,我会使用 @BoltClock's@TJ Crowder 的 解决方案。

$("div:has( > input[value='hello'] )").css("background", "red");

这使用 has-selector(docs)< /i> 选择具有 作为直接后代的

元素。

我更喜欢其他的原因是因为他们使用相当简单的有效 CSS 选择器。这是一个有效的替代方案,但可能执行速度会慢一些。

Throwing this out there for informational purposes.

In practice I'd use @BoltClock's or @T.J. Crowder's solutions.

$("div:has( > input[value='hello'] )").css("background", "red");

This uses the has-selector(docs) to select <div> elements that have a <input value="hello"> as a direct descendant.

The reason I'd prefer the others is because of the fairly simple valid CSS selectors they use. This is a valid alternative, but will likely perform a little slower.

岁月如刀 2024-10-25 10:53:56

以上答案的 @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
  )

Coffee script version of @pebbl above answer.

##############################################################################
###
    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
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文