我有一个表格,我希望所有的字段都被填写。如果单击一个字段,然后没有填写,我想显示一个红色背景。
这是我的代码:
$('#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');
}
});
无论字段是否填写,它都应用警告类。
我做错了什么?
当前回答
一个干净的css解决方案是:
input[type="radio"]:read-only {
pointer-events: none;
}
其他回答
函数checkForm() { 返回$(“输入(type =文本)”)。Filter (function () { 返回$ ().val()。长度=== 0; }) . length; }
你可以尝试这样做:
$('#apply-form input[value!=""]').blur(function() {
$(this).parents('p').addClass('warning');
});
它将只对空值的输入应用.blur()事件。
if($("#textField").val()!=null)
这对我很有用
每个人都有正确的想法,但我喜欢更明确一点,并精简价值观。
$('#apply-form input').blur(function() {
if(!$.trim(this.value).length) { // zero-length string AFTER a trim
$(this).parents('p').addClass('warning');
}
});
如果你不使用.length,那么一个'0'的条目会被标记为坏的,一个5个空格的条目可以被标记为没有$的ok。修剪。祝你好运。
一个干净的css解决方案是:
input[type="radio"]:read-only {
pointer-events: none;
}