Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery - Validation of fields with same name and class get "TypeError: e.validator.methods[o] is undefined"

I have this HTML code:

<section id="variations_holder">
    <input type="text" name="field1" class="pprice" />
    <br/>
    <input type="text" name="field2" class="pprice" />
    <br/>
    <input type="submit" />
</section>

And this is the code I'm using to handle validation:

$('#variations_holder input.pprice').each(function() {
    $(this).rules('add', {
        required: true,
        number: true,
        messages: {
            required: "Debes introducir un precio de la variación",
            number: "El precio de la variación debe ser un valor numérico o decimal"
        }
    });
});

And it works, however when the HTML looks like this (notice input has the same name's):

<section id="variations_holder">
    <input type="text" name="field1" class="pprice" />
    <br/>
    <input type="text" name="field1" class="pprice" />
    <br/>
    <input type="submit" />
</section>

The same code stop working and cause this error:

TypeError: e.validator.methods[o] is undefined

How I can fix this issue?

This is derived from this question

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Quote OP:

"... the HTML looks like this (notice input has the same name's):"

<input type="text" name="field1" class="pprice" />
<input type="text" name="field1" class="pprice" />

"How I can fix this issue?"

For the fifth time...

You can NOT have multiple input elements with the same value for every name attribute.

Because the plugin will NOT be able to keep track of the elements; there is no solution and no workaround for this specification.

(a collection or "group" of checkbox or radio elements is considered as "one data input", so they can share a name but only within the group representing this single piece of form data.)


Quote OP:

"This is derived from this question"

Where in my answer to that same question, I said:

  1. "... the jQuery Validate plugin requires that each input element contain a unique name attribute. This is how the plugin keeps track of elements. ..."

as well as in the very second comment on the OP, I said:

  1. "You cannot have multiple elements with same name. jQuery Validate requires that the name be unique."

and in a comment on my answer, I said:

  1. "However, each element must contain a unique name, no matter how the rules are created and applied."

and in my last comment on the OP, I said:

  1. "... The jQuery Validate plugin needs unique names on data input elements because that's how the plugin keeps track of them internally. There is no way around this requirement."

Also see:

Sources:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...