朋友们,
我的表格如下;
当我提交表单时,我想识别并添加一个名为“error”的类,仅用于使用jQuery具有空值的输入标签。
请帮我解决这个问题。
谢谢和问候,Rushenn。
.homework { color: red; }
$('#contact_form').submit(function(){ // listen to the submit event var err = 0; // define a varible with initial value of 0 $('input[type=text]', this).each(function(){ // iterate through the text inputs var val = $.trim(this.value); // trim and store the value // if the value is empty add a class to the previous label otherwise remove it(is it has) $(this).prev().toggleClass('homework', !val ); if (!val) err++; // if the value is empty add 1 to the err variable }) return !err; // return false if there is one or more empty field(s) })
试试这个:
$('#contact_form').submit(function() { $(this).find("input:text").each(function() { if ($.trim($(this).val()) == '') { $(this).css({ border: "2px red solid" }); return false; } }) });
$('form#contact_form').submit(function(){ if($('#name').val() == ''){ $('label[for="name"]').addClass('error'); return false; } else if($('#email').val() == ''){ $('label[for="email"]').addClass('error'); return false; } else if($('#purpose').val() == ''){ $('label[for="purpose"]').addClass('error'); return false; } return true; });
以上就是jQuery教程分享获取空输入的正确标签相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/547418.html