使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项:


1. 
    <inputtype="radio" name="testradio" value="jquery获取radio的值" />jquery获取radio的值<br/>
 
   
 
    2. 
    <inputtype="radio" name="testradio" value="jquery获取checkbox的值" />jquery获取checkbox的值<br/>
 
   
 
    3. 
    <inputtype="radio" name="testradio" value="jquery获取select的值" />jquery获取select的值<br/>


要想获取某个radio的值有以下的几种方法,直接给出代码:

1、

1. 
    $('input[name="testradio"]:checked').val();
 2、 
 
 
    1. 
    $('input:radio:checked').val();
 3、 
 
 
    1. 
    $('input[@name="testradio"][checked]');
 4、 
 
 
    1. 
    $('input[name="testradio"]').filter(':checked');
 
 
差不多挺全的了,如果我们要遍历name为testradio的所有radio呢,代码如下
 
 
 
    1. 
    $('input[name="testradio"]').each(function(){
 
   
 
    2. 
    alert(this.value);
 
   
 
    3. 
    });

如果要取具体某个radio的值,比如第二个radio的值,这样写


1. $('input[name="testradio"]:eq(1)').val()

jquery定时器

1. $(function(){  
2.     
3.       run();             //加载页面时启动定时器  
4.       var interval;  
5.           function run() {  
6.              interval = setInterval(chat, "5000");  
7.           }  
8.           function chat() {  
9.               alert("1");  
10.           }  
11.  
12.      
13.     $("#closeChat").click(function(){  
14.      
15.         clearTimeout(interval);  //关闭定时器  
16.      
17.     })  
18.            
19.   });

jQuery转义字符

因为有用到,所以记录一下。

1、class或都id中含有“.”,“#”等特殊字符

   在我们的程序中可能会遇到id中还有“.”和“#”等特殊字符,如果按照普通方式处理就会出错,解决方法是用转义符转义。

在下面程序中:

<divid="id.a">aaaaa</div>
<divid="id#b">bbbb</div>
<divid="id[1]">cccc</div>

按照我们习惯的普通方式jQuery选择器获取:

$("#id.a"),$("#id#b")              这样来获取是错误的,根本不能正确的获取

正确的方法如下:对特殊字符,转义一下

jQuery代码:

$("#id\\.a");
$("#id\\#b");
$("#id\\[1\\]");

2、属性选择器的引号问题

属性选择器中,值的引号是可能可元的,但有些特殊情况却必须有。比如:属性中含有“]”特殊字符。

<divtitle="name[1]a">aaaa</div>

如果属性值不加引号,jQuery代码:

$("div[title=name[1]a]");

会获取不到。解决方法是加上引号,正确的做法是:

$("div[title="name[1]a"]");



jQuery中:first-child的使用问题

在jQuery的子元素过滤选择器中,:first-child的使用需要注意一点,文档中说的例子是这样用的:


$("ulli:first-child").addClass("highlight");


它实现的作用是获取到所有ul下面的第一个li元素,与:first有区别。


$("ulli:first").addClass("highlight");


:first只获取一个ul中的第一个li,前提都是页面中有2个ul列表。



对于:


$("ulli:first-child").addClass("highlight");


可以使用另一种写法,效果一样,当然我觉得后者更好理解一些,因为是选择ul下的第一个子元素:


$("ul:first-child").addClass("highlight");


以上例子中的空格都需要注意。

$(function(){ 
        
//$(".one .mini:nth-child(2)").addClass("highlight");//和下面一样效果 
    
//$(".one :nth-child(2)").addClass("highlight"); 
    
 
    
//$(".one .mini:last-child").addClass("highlight");//和下面一样效果 
        
});



点击小图切换大图 html juqery代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
 <title>无标题文档</title>
 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript">
 $(function(){
  $("#smaPic a").click(function(){
   var largePath =$(this).attr("href");
   $("#bigPic").attr({ src:largePath});  return false;
  });
 });
 </script>
 <style type="text/css">
 body,ul{ padding:0; margin:0;}
 ul{ list-style:none;}
 #showPic{width:650px; height:600px; margin-left:30px auto;}
 #bigPic{ width:550px; height:400px;border:1px solid #999;padding:3px; margin-left:27px;}
 #smaPic li{ width:100px; height:100px; float:left;margin-left:10px;}
 #smaPic li img{ border:none; border:1px solid #666;padding:2px;}
 </style> </head> 
    
 <body> 
    
<divid="showPic">
  <p><imgid="bigPic"src="images/img1-lg.jpg"/></p>
  <ulid="smaPic">
  <li><ahref="images/img2-lg.jpg"><imgsrc="images/img2-thumb.jpg"/></a></li>
  <li><ahref="images/img3-lg.jpg"><imgsrc="images/img3-thumb.jpg"/></a></li>
  <li><ahref="images/img4-lg.jpg"><imgsrc="images/img4-thumb.jpg"/></a></li>
  <li><ahref="images/img5-lg.jpg"><imgsrc="images/img5-thumb.jpg"/></a></li>
  <li><ahref="images/img6-lg.jpg"><imgsrc="images/img6-thumb.jpg"/></a></li>
  </ul>
  </div>
 </body>
 </html>