新人:关于jq检测复选框的状态,怎么就不正确呢?求帮瞧瞧
jquery吧
全部回复
仅看楼主
level 1
专注弹力球
楼主
2016年01月11日 09点01分
1
level 1
专注弹力球
楼主
我希望在点击提交按钮后,如果复选框没选中给出一个提示,然而现在不论选不选中都给提示。。。而且自己定义的state变量也不显示复选框的状态,显示undefined...
2016年01月11日 09点01分
2
level 1
专注弹力球
楼主
代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TestJQCheckBox</title>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript">
$().ready(function()
{
$("input:eq(1)").click(function()
{
alert(1); //能显示1
var state=$("input:eq(0)").attr("checked");
alert(state); //显示undefined
if($("input:eq(0)").attr("checked")!="checked")
{
alert("你必须同意网站服务条款。"); //选中或不选复选框都会提示
}
});
});
</script>
</head>
<body>
<form>
<input type="checkbox" name="ok">同意<br>
<input type="submit" value="提交">
</form>
</body>
</html>
2016年01月11日 09点01分
3
level 1
越南皇子高地平
我给你简单改了下 你看看是不是这种效果
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TestJQCheckBox</title>
<script type="text/javascript" src="scripts/jquery-1.11.3.js"></script>
</head>
<body>
<input type="checkbox" id="myCheckBox"/><label for="myCheckBox">同意</label>
<input type="button" value="提交" />
<script type="text/javascript">
$(function(){
$('input[type="button"]').click(function(){
if($('#myCheckBox').is(':checked')) {
//change it to alert('Its Checked'); if you not working with console
alert('提交成功');
}else {
alert('没有选中');
}
})
});
</script>
</body>
</html>
2016年01月12日 02点01分
4
level 8
hashak☞
你试试prop方法。
$().ready(function(){
$("input:eq(1)").click(function(){
alert(1); //能显示1
var state=$("input:eq(0)").prop("checked");//prop('checked',true);
alert(state); //显示undefined
if($("input:eq(0)").prop('checked')==false){
alert("你必须同意网站服务条款。");
}
});
});
2016年01月12日 04点01分
5
1