jquery判断是否选中单选按钮的问题,请前辈们给看看

liangphp 2011-11-17 09:44:05

<form>
性别
<input type="radio" name="sex"> 男
<input type="radio" name="sex"> 女
爱好
<input type="radio" name="enjoy">看书
<input type="radio" name="enjoy">逛街
<input type="radio" name="enjoy">运动
政治面貌
<input type="radio" name="zhengzhi">团员
<input type="radio" name="zhengzhi">党员
<input type="submit" value="提交">
</form>


下面是我写的jquery,当所有选项都选中的时候,我才提交表单,有一项未选中都不能提交表单,请各位看看我哪错了.怎么修改,非常感谢




function toSubmit()
{
var s=true;
$(":radio").each(function(){

var radio = $(this).attr("name");

if($(":radio[name='"+radio+"']").attr("checked")==false)
{
s=false;
}

})
if(s==false)
{
alert("所有项为必填!");
}

return s;

...全文
900 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
kanglujiushi 2012-07-16
  • 打赏
  • 举报
回复
遇到同样的问题,大侠快来解决
默默不得鱼 2011-11-17
  • 打赏
  • 举报
回复
alert($(":radio:checked").length)
liangphp 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liujiebest 的回复:]
直接判断选中按钮的个数,小于3个都不能提交

JScript code
var num=$("input[type=radio]":checkd).length;
if(num<3)
{
alert("所有项为必填!");
}
[/Quote]
var num=$("input[type=radio]":checkd).length;这行咋报错呢
_懒猫 2011-11-17
  • 打赏
  • 举报
回复
直接判断选中按钮的个数,小于3个都不能提交
var num=$("input[type=radio]":checkd).length;
if(num<3)
{
alert("所有项为必填!");
}
livesguan 2011-11-17
  • 打赏
  • 举报
回复
$(":radio[name='"+radio+"']"):找到type为radio的input标签,[attribute=value]进一步过滤,找到该属性等于value的标签.但是看你的标签里没有name为radio的标签啊.

三石-gary 2011-11-17
  • 打赏
  • 举报
回复
你这个根本不可能全选中啊。。。
liangphp 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kk3k2005 的回复:]
if($(":radio[name='"+radio+"']").attr("checked")==false)
{
s=false;
return false;
}
[/Quote]
前辈,不行啊,当我全部选中的时候还是不能提交
峭沙 2011-11-17
  • 打赏
  • 举报
回复
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script src="jquery.js" ></script>
</head>
<body>
<form>
性别
<input type="radio" name="sex"> 男
<input type="radio" name="sex"> 女
爱好
<input type="radio" name="enjoy">看书
<input type="radio" name="enjoy">逛街
<input type="radio" name="enjoy">运动
政治面貌
<input type="radio" name="zhengzhi">团员
<input type="radio" name="zhengzhi">党员
<input type="submit" value="提交">
</form>
<script>
function toSubmit(){
var result = true;
var radioSets = {};
$(":radio").each(function(){
var name = $(this).attr("name");
if(!radioSets[name]){
radioSets[name] = [];
}
radioSets[name].push($(this));
});
$.each(radioSets, function(i, radios){
$.each(radios, function(j, radio){
if(radio.prop('checked')){
return false;
}else if(j === radios.length - 1){
result = false;
alert(i+"未填!");
}
});
});
return result;
}
$(":submit").click(toSubmit);
</script>
</body>
</html>
KK3K2005 2011-11-17
  • 打赏
  • 举报
回复
if($(":radio[name='"+radio+"']").attr("checked")==false)
{
s=false;
return false;
}
_懒猫 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 liangphp 的回复:]

引用 6 楼 liujiebest 的回复:
直接判断选中按钮的个数,小于3个都不能提交

JScript code
var num=$("input[type=radio]":checkd).length;
if(num<3)
{
alert("所有项为必填!");
}

var num=$("input[type=radio]":checkd).length;这行咋报错呢……
[/Quote]
杯具了。。。var num=$("input[type=radio]:checkd").length;
风一样的大叔 2011-11-17
  • 打赏
  • 举报
回复
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script src="jquery-1.4.2.js"></script>
</head>
<body>
<form>
性别:
<input type="radio" name="sex">

<input type="radio" name="sex">
女<br />
爱好:
<input type="radio" name="enjoy">看书
<input type="radio" name="enjoy">逛街
<input type="radio" name="enjoy">运动<br />
政治面貌:
<input type="radio" name="zhengzhi">团员
<input type="radio" name="zhengzhi">党员<br />
<input type="submit" value="提交">
</form>
<script>
function toSubmit() {
var count = 0;
$("input[type=radio]:checked").each(function () {
count++;
});
if (count != 3) {
alert("所有项为必填!");
}
}
$(":submit").click(toSubmit);
</script>
</body>
</html>
liangphp 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ifandui 的回复:]
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>……
[/Quote]

好了好了,我刚刚清了一下缓存...非常感谢您
默默不得鱼 2011-11-17
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
<script type="text/javascript" src="../script/jquery-1.7.js"></script>
<script type="text/javascript">
window.onload=function(){
$("#b1").click(function(){
alert($(":radio:checked").length);
});
};
</script></head>
<body>
<input type="radio" name="sex"> 男
<input type="radio" name="sex"> 女
爱好
<input type="radio" name="enjoy">看书
<input type="radio" name="enjoy">逛街
<input type="radio" name="enjoy">运动
政治面貌
<input type="radio" name="zhengzhi">团员
<input type="radio" name="zhengzhi">党员


<input type="button" id="b1" value="click me" />
</body>
</html>
怎么可能会出不来
liangphp 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ifandui 的回复:]
alert($(":radio:checked").length)
[/Quote]
alert不出来啊,$(":radio:checked").length不对吧...

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧