我有一个表格,我希望所有的字段都被填写。如果单击一个字段,然后没有填写,我想显示一个红色背景。
这是我的代码:
$('#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;
}
其他回答
<script type="text/javascript">
$('input:text, input:password, textarea').blur(function()
{
var check = $(this).val();
if(check == '')
{
$(this).parent().addClass('ym-error');
}
else
{
$(this).parent().removeClass('ym-error');
}
});
</script>// :)
如何检查null undefined和空在jquery
$(document).on('input', '#amt', function(){
let r1;
let r2;
r1 = $("#remittance_amt").val();
if(r1 === undefined || r1 === null || r1 === '')
{
r1 = 0.00;
}
console.log(r1);
});
函数checkForm() { 返回$(“输入(type =文本)”)。Filter (function () { 返回$ ().val()。长度=== 0; }) . length; }
试试这个:
function empty(){
if ($('.text').val().length == 0)
{
alert("field should not be empty");
}
}
$('#apply-form input').blur(function()
{
if( !$(this).val() ) {
$(this).parents('p').addClass('warning');
}
});
你不一定需要。length,或者看看它是否为>0,因为空字符串的值无论如何都是假的,但如果你为了可读性考虑:
$('#apply-form input').blur(function()
{
if( $(this).val().length === 0 ) {
$(this).parents('p').addClass('warning');
}
});
如果你确定它总是操作文本字段元素,那么你可以使用this.value。
$('#apply-form input').blur(function()
{
if( !this.value ) {
$(this).parents('p').addClass('warning');
}
});
此外,你应该注意$('input:text')抓取多个元素,指定一个上下文或使用this关键字,如果你只是想引用一个单独的元素(前提是在上下文的后代/子代中有一个文本字段)。