简体   繁体   English

jQuery在提交按钮单击时显示处理图像,但如果表单中存在css错误类,则将其隐藏

[英]jquery show processing image on submit button click but hide if css error class exists in the form

I have the following jQuery code: 我有以下jQuery代码:

$("#signUpFormSubmitButton").click(function(){
$('#processing').show();
});

This works fine. 这很好。 When the submit button is clicked on the form, I show a processing spinning graphic. 在表单上单击“提交”按钮后,我将显示一个正在处理的旋转图形。

I don't want to show the processing spinning graphic however, if there are errors on the form (form is not yet submitted). 但是,如果表单上有错误(尚未提交表单),我不想显示正在处理的旋转图形。 I validate my form with jQuery as well and I add a "error" css class to the form input fields if there is an error. 我也使用jQuery验证表单,如果有错误,则会向表单输入字段添加“错误” css类。

I want to use the css error class as the flag for this. 我想使用css错误类作为对此的标志。

I tried: 我试过了:

if (!$("#myForm input").hasClass("error") ) {
    $('#processing').show();
}

But did not work. 但是没有用。 Any help greatly appreciated. 任何帮助,不胜感激。

Try using the toggle() method and a simple selector 尝试使用toggle()方法和一个简单的选择器

$("#signUpFormSubmitButton").click(function(){
    $('#processing').toggle($("#myForm input.error").length > 0);
});

toggle(showOrHide) 切换(showOrHide)

showOrHideA Boolean indicating whether to show or hide the elements. showOrHideA布尔值,指示是否显示或隐藏元素。

if ($("#myForm input").hasClass("error") ) {
      //your error code here }
else {
      //your loading image
$('#processing').show(); }

You could try counting the number of elements with the .error class: 您可以尝试使用.error类计算元素的数量:

if ($("#myForm input.error").length != 0)
{
    $('#processing').hide();
} else {
  // Show your spinner here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM