jQuery Validation Plugin - How to show labels on page load

安稳与你 提交于 2019-12-12 20:24:21

问题


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">&nbsp;</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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!