【问题标题】:Jquery validate is losing styles on submissionJquery validate 在提交时丢失样式
【发布时间】:2017-04-27 16:36:20
【问题描述】:

我正在使用jquery validate插件,当表单提交时,样式丢失了。

他们只是变得古怪!

我希望表单在提交表单之前保持原样。

附上截图和代码:

<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>

<style>
.label {
  width:100px;
  text-align:right;
  float:left;
  padding-right:10px;
  font-weight:bold;
}
#ats_fellow_application_payment_form label.error {
  color:#FB3A3A;
  font-weight:bold;
  padding: 10px;
}

#ats_fellow_application_payment_form input.error {
  color:#FB3A3A;
  font-weight:bold;
  padding: 0px;
}

h1 {
  font-family: Helvetica;
  font-weight: 100;
  color:#333;
  padding-bottom:20px;
}
</style>


<script type="text/javascript">
// Wait for the DOM to be ready
$(function() {
  // Initialize form validation on the registration form.
  // It has the name attribute "registration"
  $("form[name='ats_fellow_application_payment_form']").validate({
    // Specify validation rules
    rules: {
      // The key name on the left side is the name attribute
      // of an input field. Validation rules are defined
      // on the right side
      firstname: "required",
      lastname: "required",
      email: {
        required: true,
        // Specify that email should be validated
        // by the built-in "email" rule
        email: true
      }
    },
    // Specify validation error messages
    messages: {
      firstname: "Please enter your firstname",
      lastname: "Please enter your lastname",

      email: "Please enter a valid email address"
    },
    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) {
      form.submit();
    }
  });
});
</script>


<form  method="POST" name="ats_fellow_application_payment_form" id="ats_fellow_application_payment_form" novalidate="novalidate">   
    <div class="label">First Name</div><input type="text" id="firstname" name="firstname" /><br />
    <div class="label">Last Name</div><input type="text" id="lastname" name="lastname" /><br />
    <div class="label">Email</div><input type="text" id="email" name="email" /><br />
    <div style="margin-left:140px;"><input type="submit" name="submit" value="Submit" /></div>
</form>

【问题讨论】:

  • 我认为这是由 validate 插件添加的错误 label 元素引起的。我建议您查看验证插件允许您使用的错误自定义方法
  • 您的 OP 中的 CSS 代码不会重现您描述的问题:jsfiddle.net/j6v3x378 ~ 但是,jQuery Validate 插件根本不会影响 CSS;它只是为错误消息添加/删除类和label 元素。检查 DOM 并创建正确的 CSS 完全取决于您。

标签: jquery css jquery-validate


【解决方案1】:

您的代码有效。这是一个CSS问题。我建议删除 label.error 上的顶部/底部填充,然后删除或更新 input.error。根据您的图像,您提供的代码中未包含其他样式添加到错误标签中。

或者,您可以使用 validate 的 errorContainer、errorLabelContainer 和 wrapper 选项将错误消息添加到错误容器中,从而更改错误消息的位置。您还可以使用以下方法将错误消息附加到元素以控制样式和位置:

errorPlacement: function (error, element) {
    error.appendTo(element.siblings('span.errorMsg'));
}

可以在docs中找到有关这些选项的更多信息

工作fiddle

【讨论】:

  • 仅供参考 - OP 的代码与您的代码没有太大区别:jsfiddle.net/j6v3x378 ~ 他问题的根源在于他未能向我们展示的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 2014-06-16
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多