Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following markup, and I want to make the All radio button checked.

<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>

I'd like to match via attribute, but I need to match on 2 attributes, @name='Foo' and @value='All'.

Something like this:

$("input[@name='Foo' @value='all']").attr('checked','checked');

Can someone show how this can be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
302 views
Welcome To Ask or Share your Answers For Others

1 Answer

The following HTML file shows how you can do this:

<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>

When you click on the link, the desired radio button is selected. The important line is the one setting the checked attribute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...