我有一个表格,我希望所有的字段都被填写。如果单击一个字段,然后没有填写,我想显示一个红色背景。
这是我的代码:
$('#apply-form input').blur(function () {
if ($('input:text').is(":empty")) {
$(this).parents('p').addClass('warning');
}
});
无论字段是否填写,它都应用警告类。
我做错了什么?
我有一个表格,我希望所有的字段都被填写。如果单击一个字段,然后没有填写,我想显示一个红色背景。
这是我的代码:
$('#apply-form input').blur(function () {
if ($('input:text').is(":empty")) {
$(this).parents('p').addClass('warning');
}
});
无论字段是否填写,它都应用警告类。
我做错了什么?
当前回答
为什么没有人提到
$(this).filter('[value=]').addClass('warning');
在我看来更像jquery
其他回答
一个干净的css解决方案是:
input[type="radio"]:read-only {
pointer-events: none;
}
:empty伪选择器用于检查元素是否不包含子元素,你应该检查它的值:
$('#apply-form input').blur(function() {
if(!this.value) { // zero-length string
$(this).parents('p').addClass('warning');
}
});
试试这个:
function empty(){
if ($('.text').val().length == 0)
{
alert("field should not be empty");
}
}
if($("#textField").val()!=null)
这对我很有用
在模糊上做太有限了。它假设表单字段是重点,所以我更喜欢在提交时执行,并通过输入进行映射。在处理了多年花哨的模糊、聚焦等技巧后,让事情变得更简单会在重要的地方产生更多的可用性。
$('#signupform').submit(function() {
var errors = 0;
$("#signupform :input").map(function(){
if( !$(this).val() ) {
$(this).parents('td').addClass('warning');
errors++;
} else if ($(this).val()) {
$(this).parents('td').removeClass('warning');
}
});
if(errors > 0){
$('#errorwarn').text("All fields are required");
return false;
}
// do the ajax..
});