使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项:
1.
<
input
type
=
"radio"
name
=
"testradio"
value
=
"jquery获取radio的值"
/>jquery获取radio的值<
br
/>
2.
<
input
type
=
"radio"
name
=
"testradio"
value
=
"jquery获取checkbox的值"
/>jquery获取checkbox的值<
br
/>
3.
<
input
type
=
"radio"
name
=
"testradio"
value
=
"jquery获取select的值"
/>jquery获取select的值<
br
/>
要想获取某个radio的值有以下的几种方法,直接给出代码:
1、
1.
$(
'input[name="testradio"]:checked'
).val();
1.
$(
'input:radio:checked'
).val();
1.
$(
'input[@name="testradio"][checked]'
);
1.
$(
'input[name="testradio"]'
).filter(
':checked'
);
差不多挺全的了,如果我们要遍历name为testradio的所有radio呢,代码如下
1.
$(
'input[name="testradio"]'
).each(
function
(){
2.
alert(
this
.value);
3.
});
1.
$(
'input[name="testradio"]:eq(1)'
).val()
jquery定时器
- $(function(){
-
-
run(); //加载页面时启动定时器 -
var interval; -
function run() { -
interval = setInterval(chat, "5000"); -
} -
function chat() { -
alert("1"); -
} -
-
-
$("#closeChat").click(function(){ -
-
clearTimeout(interval); //关闭定时器 -
-
}) -
-
});
jQuery转义字符
因为有用到,所以记录一下。
转自网址:http://lansky07.javaeye.com/blog/283875
1、class或都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(){
});
点击小图切换大图 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"
<script type="text/javascript">
$(function(){
});
</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>
<body>
</body>
</html>