简体   繁体   English

jQuery validate()语法错误? 加载?

[英]jQuery validate() error on syntax? load?

I'm using the jQuery validator to validate a form on my application. 我正在使用jQuery验证程序来验证应用程序上的表单。 This is all the code I used to load and run it: 这是我用来加载和运行它的所有代码:

check_in.js check_in.js

$(window).bind("load", function() {
  jQuery(function() {

    $('#edit-reservation-form').validate({
      highlight: function(element) {
        $(element).fadeOut(100, function() {
          $(element).addClass("required-input");
          $(element).fadeIn();
        });
      },
      unhighlight: function(element) {
        $(element).removeClass("required-input");
      }
    });

    // To set default messages for validator.
    jQuery.extend(jQuery.validator.messages, {
        required: "Custom text message."
    });
});

And in my application.html.slim 在我的application.html.slim中

= javascript_include_tag "application", "data-turbolinks-track" => true
script[async src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"]

And in my check_in_form.html.slim (not real name, just representative) for each field that I want to be required I have 'data-rule-required': true 在我想要的每个字段的check_in_form.html.slim(不是真实姓名,仅是代表名称)中,我有'data-rule-required': true需要'data-rule-required': true

Now, this works perfectly in development, and production, both locally and on the machines QA use. 现在,无论是在本地还是在QA使用的机器上,这在开发和生产中都能完美运行。 HOWEVER, on the client machine it is not working properly (he's using Chrome but I'm not sure it's the latest version). 但是,在客户端计算机上,它无法正常运行(他使用的是Chrome,但我不确定它是否为最新版本)。 I don't know exactly the error, but for what he said (and showed on a screenshot), when he leaves a field blank, it's like the script is failing and then it is not redirecting to where it should. 我不确切知道错误,但是对于他说的话(并显示在屏幕截图上),当他将字段留空时,就好像脚本失败了,然后就没有重定向到应该去的地方。 As if the script had a syntax error or something is not entirely loaded. 好像脚本有语法错误或某些内容未完全加载。

The only thing I could come up (but haven't tried yet) is to change the first line of the .js to: 我唯一能想到的(但尚未尝试过)是将.js的第一行更改为:

$(document).ready(function() {

And the loading on application.html.slim to 并在application.html.slim上加载

= javascript_include_tag "application", "data-turbolinks-track" => true, async: Rails.env.production?
script[src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.min.js"]

Any ideas on what might be? 有什么想法吗?

Your option is good, with the $(document).ready(function() { . But try also to do this: 您可以选择$(document).ready(function() { 。),但也可以尝试这样做:

var $form = $('#edit-reservation-form');
var validator = $form.validate(//logic here);

I believe you didn't actually declare validator . 我相信您实际上并没有宣布validator And maybe you had some problems here: 也许您在这里遇到了一些问题:

// To set default messages for validator.
    jQuery.extend(jQuery.validator.messages, {
        required: "Custom text message."
    });

Try this and let's see how it goes. 试试看,看看它如何进行。 Anyhow, a tip to debug Javascript, or at least what I usually use is FireBug in Firefox. 无论如何,调试Java技巧或至少我通常使用的技巧是Firefox中的FireBug。 I actually only have Firefox installed to debug Javascript. 我实际上只安装了Firefox来调试Javascript。

Let me know how it goes! 让我知道事情的后续! Good luck! 祝好运!

If you're using Turbolinks it could be a problem to only use the ready event, because is not fired after the first page load of your app, so you need to bind to custom turbolinks events like page:load like: 如果您使用的是Turbolinks,则仅使用ready事件可能是个问题,因为在应用程序的第一页加载后不会触发,因此您需要绑定到自定义的turbolinks事件,例如page:load like:

var ready;
ready = function() {
    $('#edit-reservation-form').validate({
        highlight: function(element) {
            $(element).fadeOut(100, function() {
                $(element).addClass("required-input");
                $(element).fadeIn();
            });
        },
        unhighlight: function(element) {
            $(element).removeClass("required-input");
        }
    });

    // To set default messages for validator.
    jQuery.extend(jQuery.validator.messages, {
        required: "Custom text message."
    });
};

$(document).ready(ready);
$(document).on('page:load', ready);

Let me know if this helps! 让我知道这是否有帮助!

For Rails 5 the event name changed to turbolinks:load 对于Rails 5 ,事件名称更改为turbolinks:load

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

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