<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;
解决方案 »
- Ext.ux是什么东西 ext中没有这个包啊,新手求人给解释下 谢谢
- 弹出窗口 要怎样才能实现alert那样的居中效果?
- 麻烦大家看看这个打印控件如何?
- 【mootools】request中的send中的open问题
- JavaScript获取连接<a ID=???></a> 标签ID的值
- 在提交表单之后,如何还保存文本框里的内容
- 有关聊天室的问题,请高手指教!
- 请问"var p=tdObj.previousSibling;"中previousSibling是何含义?(javascript中)
- 求助一个列表树JS传参问题
- 百度地图问题!!(高分奖赏)
- jquery-1.6.4.min.js和jquery.min.js有什么区别
- 怎样才能在手机浏览器上获取坐标值
{
s=false;
return false;
}
<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>
var num=$("input[type=radio]":checkd).length;
if(num<3)
{
alert("所有项为必填!");
}
<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>
怎么可能会出不来
<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>