is('checked')筛选方法工作不正常
is('checked') filtering method not working correctly
我正在尝试查看is()
方法是否选中了一个复选框,但它给出了一个意外的结果。无论哪种方式,如果我是否选中复选框,该方法都将返回false。
HTML
<form action="" method="post" id="place_order">
<input type="checkbox" name="tos" class="required check-condition" />
<span class="error error-tos" style="display:none">* This field is required</span><br>
<input type="submit" name="place_order" value="Submit Order" class="order-sb-btn" />
</form>
Jquery
$("#place_order").submit(function () {
var is_tos_checked = $(".check-condition").is('checked');
console.log(is_tos_checked);
if (is_tos_checked) {
$(".error-tos").hide();
return true;
} else {
$(".error-tos").show();
return false;
}
});
Jsfidle
http://jsfiddle.net/yogc5ypb/1/
您需要将checked表示为伪选择器,即前缀为:
.is(':checked');
我通常使用.prop('checked')
因此,在您的代码中,您可以将行更改为:
var is_tos_checked = $(".check-condition").prop('checked');
如果您想在用户选中复选框的情况下提交表单,那么您也可以使用以下方法-
1.在html文件中写入以下语句
<form action="" method="post" id="place_order">
<input type="checkbox" name="tos" class="required check-condition" />
<span class="error error-tos" style="display:none">* This field is required</span><br>
<input type="submit" name="place_order" value="Submit Order" class="order-sb-btn" />
</form>
2.在js文件中编写以下语句。
jQuery('.order-sb-btn').click(function(){
var checkedValue = $('.required:checked').val();
if(checkedValue == undefined)
{
alert('not checked');
return false;
}
else
{
alert('checked');
}
})
通过使用上述代码,如果用户选中复选框,表单将被提交。要查看示例,请参阅以下链接-
http://jsfiddle.net/1eygh774/1/
相关文章:
- Javascript form.submit()方法是如何工作的
- JavaScript中的这个call()-方法是如何工作的
- Jquery ajax方法请求体为null,但poster工作正常
- 我该如何阻止单击方法多次工作
- jQuery方法未按预期工作-toggleClass
- 如何让应用程序帮助程序方法在发送的请求为 JS 格式时工作
- jQuery on() 方法只能通过 childSelector 间接工作
- 如何制作Javascript's减少对数组数组的方法工作
- 如何使我自己的javascript Math方法工作
- * 方法工作正常,但不能使用 jQuery ajax
- Javascript调用同一对象内的对象方法工作不正常
- 当项目停止在.NET中运行时,有没有办法阻止web方法工作
- is('checked')筛选方法工作不正常
- 在操作DOM时无法使对象方法工作
- ie9 getAttribute方法工作
- 如何使一个方法工作- Javascript
- Javascript变量到php变量转换方法工作.但我无法比较php变量
- 为什么'我的start()和stop()方法工作吗
- 从数据库加载值的Onchange Ajax方法工作正常,但如何将文本区域值与另一个文本框值相乘
- JavaScript RegExp测试方法工作不稳定,我的意思是第一次执行和第二次执行的结果不同