问题
The plugin inserts a label when a input has been filled out, however I want all the labels to be inserted on page load. Any ideas would be great, thanks.
The link to the plugin is here.
http://jqueryvalidation.org/documentation/
I'm thinking there is a method I can use to just insert all the labels.
An example of the code would be as follows.
On page Load a field might look like this...
<div id="name-field" class="field input-text">
<input type="text" id="name" name="name" onblur="if (this.value=='') this.value = 'Name'" onfocus="if (this.value=='Name') this.value = ''" value="Name" class="requiredAstrix" />
<div id="name-error-label" class="error-label f3a cf98">
This field is required
<div class="arrow s14"></div>
</div>
<div id="name-hover-label" class="hover-label cf2"></div>
</div>
Onblur the plugin inserts a label as per below... (this is how I want all the fields to be on page load)
<div id="name-field" class="field input-text">
<input type="text" id="name" name="name" onblur="if (this.value=='') this.value = 'Name'" onfocus="if (this.value=='Name') this.value = ''" value="Name" class="requiredAstrix" />
<label for="name" class="error checked"> </label>
<div id="name-error-label" class="error-label f3a cf98">
This field is required
<div class="arrow s14"></div>
</div>
<div id="name-hover-label" class="hover-label cf2"></div>
</div>
回答1:
Just use the .valid() method to perform the validation test programatically on DOM ready.
$(document).ready(function() {
$('name-field').validate({ // initialize the plugin on your form
// rules,
// messages,
// options,
// callback functions
});
$('name-field').valid(); // immediately fire validation on DOM ready
});
Working DEMO: http://jsfiddle.net/JVzKd/
来源:https://stackoverflow.com/questions/17964510/jquery-validation-plugin-how-to-show-labels-on-page-load