jQuery匹配多个属性

zad*_*dam 45 html jquery jquery-selectors

我有以下标记,我想All检查单选按钮.

<ul>
    <li><input type="radio" value="All" name="Foo"/>All</li>
    <li><input type="radio" value="New" name="Foo"/>New</li>
    <li><input type="radio" value="Removed" name="Foo"/>Removed</li>
    <li><input type="radio" value="Updated" name="Foo"/>Updated</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我想匹配via属性,但我需要匹配2个属性,@name='Foo'@value='All'.

像这样的东西:

$("input[@name='Foo' @value='all']").attr('checked','checked');
Run Code Online (Sandbox Code Playgroud)

有人可以说明如何做到这一点?

pax*_*blo 68

以下HTML文件显示了如何执行此操作:

<html>
  <head>
    <script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        $("a").click(function(event){
          $("input[name='Foo'][value='All']").attr('checked','checked');
          event.preventDefault();
        });
      });
    </script>
  </head>
  <body>
    <ul>
      <li><input type="radio"  value="All"  name="Foo"  />All</li>
      <li><input type="radio"  value="New"  name="Foo"  />New</li>
      <li><input type="radio"  value="Removed"  name="Foo"  />Removed</li>
      <li><input type="radio"  value="Updated"  name="Foo"  />Updated</li>
    </ul>
    <a href="" >Click here</a>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

单击链接时,将选择所需的单选按钮.重要的一行是设置checked属性的那一行.


eas*_*sel 13

我正在对着类似于此的墙敲打我的脑袋,并且只想指出在jQuery 1.3中,接受的答案中使用的语法是唯一可用的语法.提问者使用@语法来表达jQuery中根本不起作用的表达式.希望这有助于下一个人通过Google = p遇到这个问题

要清楚,你必须使用

jQuery('input[name=field1][val=checked]') 
Run Code Online (Sandbox Code Playgroud)

并不是

jQuery('input[@name=field1][@val=checked]')
Run Code Online (Sandbox Code Playgroud)