简体   繁体   English

jQuery更改在浏览器中的行为不一致

[英]JQuery change behaving inconsistently across browsers

I'm new to JavaScript and I'm having a bit of a problem with some JQuery.validate code that I've inherited. 我是JavaScript的新手,我继承了一些JQuery.validate代码时遇到了一些问题。 Please forgive me if the answer is obvious :) 如果答案很明显,请原谅我:)

I have the following validation method declaration: 我有以下验证方法声明:

jQuery.validator.addMethod("myMethod", function(value, element, param) {
    /*...*/
    if (document.getElementById("DOMID") {
        /*...*/
    }
    /*...*/
)};

Essentially the method is checking for the existence of a certain DOM object and it sets some different validation parameters if it does exist. 本质上,该方法是检查某个DOM对象是否存在,如果存在,它会设置一些不同的验证参数。 The object I want to validate is a set of <select> inputs wrapped in a parent <div id="DOMID"> , but there are a couple of different "DOMID" cases which each have slightly different validation rules - hence the check inside the method. 我要验证的对象是包装在父<div id="DOMID">一组<select>输入,但是有几种不同的“ DOMID”用例,每种用例都有略微不同的验证规则-因此内部检查方法。 I want to validate the <select> s each time one of them changes their value. 我想在每次<select>更改其值时对其进行验证。 I then have this activation: 然后,我有此激活:

$(".myClass").validate({
    onchange: true,
});

With myClass being applied to all of the <select> inputs I want to validate. myClass应用于所有要验证的<select>输入。

Lastly, I have this registration: 最后,我有此注册:

jQuery.validator.addClassRules(
{
   myClass:
     {
      myMethod: true,
     }
}

This works just fine in Chrome - validation is applied each time I change one of the <select> options. 这在Chrome中效果很好-每次更改<select>选项之一时都会应用验证。

However, in Firefox, this is not the case - I find that I have to blur the select in order for the validation to activate. 但是,在Firefox中并非如此-我发现我必须模糊选择才能激活验证。 So, that's one question: Why does change behave differently between Chrome and Firefox? 因此,这是一个问题: 为什么Chrome和Firefox的更改行为有所不同? Or is that not the case, and there's something else in my code which is responsible for this inconsistency? 还是不是这样,并且我的代码中还有其他原因导致这种不一致?

The second question I have is about something I find quite peculiar. 我的第二个问题是关于我觉得很奇怪的事情。 In attempting to fix the issue of Firefox not validating until blur, I changed my JavaScript getElementByID... to use jQuery, instead: 为了解决Firefox直到模糊才验证的问题,我将JavaScript getElementByID...更改为使用jQuery,改为:

if ($("#DOMID").length > 0) { [...]

(I also tried if ($("#DOMID").get(0)) but they do the same thing, I think - unfortunately, neither made Firefox behave properly) (我也尝试过if ($("#DOMID").get(0))但是我认为它们做同样的事情-不幸的是,它们都没有使Firefox正常运行)

The weird part is that after I made this change, Chrome stopped validating on change and would only validate on blur. 奇怪的是,在我进行了更改之后, Chrome停止了对更改的验证,仅对模糊进行了验证。 That's the only line of code I made a change to - I've since switched it back and Chrome behaves properly again. 那是我所做的更改的唯一代码行-自从将其切换回去之后,Chrome再次正常运行。 Why does that happen? 为什么这样? How would the internals of the validation method affect when it was being called? 验证方法的内部调用将如何影响?

Hopefully this question makes sense - please let me know if any additional information would be helpful! 希望这个问题有意义-请让我知道是否有任何其他信息会有所帮助! Also any words of wisdom for debugging this problem would be very appreciated. 同样,对于调试该问题的任何明智之言也将不胜感激。 Thanks very much for any advice! 非常感谢您的任何建议!

You're using the jQuery Validate plugin improperly... 您使用的jQuery Validate插件不正确...

Quote OP : 引用OP

"I want to validate the <select> s each time one of them changes their value. I then have this activation:" “每当其中一个更改其<select>值时,我都希望对其进行验证。然后,我将进行此激活:”

$(".myClass").validate({
    onchange: true,
});

1) You cannot attach .validate() to any single input element. 1)您不能将.validate()附加到任何单个输入元素。 The .validate() method can only be attached to the <form> element ... .validate()方法只能附加到<form>元素 ...

$('#myForm').validate({
    // options, rules, etc.
});

2) There is no such plugin option called onchange . 2) 没有这样的插件选项onchange You are only allowed to use the options that were created by the developer specifically for this plugin . 仅允许您使用开发人员专门为此插件创建的选项


If you want to add and remove rules based on a select element, then use the .rules('add') and .rules('remove') methods . 如果要基于select元素添加和删除规则,请使用.rules('add').rules('remove')方法

$('#myselect').on('change', function() {
    $('#myInput').rules('add', {  // add rule(s) to #myInput
        required: true
    });
}); 

If instead you want to "validate" or test the validity of something, use the .valid() method . 相反,如果您要“验证”或测试某些内容的有效性,请使用.valid()方法

$('#myselect').on('change', function() {
    $('#myInput').valid();  // trigger a test of #myInput
});

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

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