简体   繁体   English

jQuery验证插件帮助

[英]JQuery Validation Plugin help

I am trying to use the validation plugin for JQuery to validate a form, but I want it to not only show an error message, but also change the css for the td above it. 我正在尝试使用JQuery的验证插件来验证表单,但我希望它不仅显示错误消息,还希望更改其上方td的css。 I currently have 我目前有

function myErrorHandle(element) {
    element.closest("td").addClass("fsValidationError");
}
$("#fsForm691575").validate({
    errorPlacement: myErrorHandle,
    rules: {
        field7014601: {
            required: true
        }
});

Where fsForm691575 is my form and field7014601 is just a name field. 其中fsForm691575是我的表单,而field7014601只是一个名称字段。 I just sort of hacked the myErrorHandle function together from another suggestion I found, so if I can do away with that but still get the desired effect, that would be nice. 我只是从发现的另一条建议中窃取了myErrorHandle函数,因此,如果我可以消除它,但仍然获得所需的效果,那就太好了。

Thanks 谢谢

I can't cater an answer specific to your case (no HTML), but I can show you how to change the default "highlight" action of validate. 我无法满足特定于您的情况的答案(没有HTML),但可以向您展示如何更改验证的默认“突出显示”操作。

For example, given the following HTML: 例如,给定以下HTML:

<form action="#">
  <table>
    <tr>
      <td>
        <input id='firstname' name='firstname' class='required' />
      </td>
    </tr>
    <tr>
      <td>
        <input id='lastname' name='lastname' class='required' />
      </td>
    </tr>
  </table>
  <input type="submit" value="submit" />
</form>

You could configure validate to color the parent tr red: 您可以配置验证以将父tr红色:

$("form").validate({
  errorClass: "error",
  highlight: function(element, errorClass, validClass) {
    $(element).closest("tr")
      .addClass(errorClass)
      .removeClass(validClass);
  },
  unhighlight: function(element, errorClass, validClass) {
    $(element).closest("tr")
      .removeClass(errorClass)
      .addClass(validClass);
  }
});

Example: http://jsbin.com/afoju5/3/edit 示例: http//jsbin.com/afoju5/3/edit

If you need even more granular control over what happens when you show errors, check out the showErrors option. 如果您需要更详细地控制显示错误时发生的情况,请查看showErrors选项。

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

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