JQuery Validate input file type

后端 未结 3 1833
孤独总比滥情好
孤独总比滥情好 2020-12-09 11:57

I have a form that can have 0-hundreds of elements. I have named them sequentially depending on how many are dynamically added to th

3条回答
  •  暖寄归人
    2020-12-09 12:43

    Simply use the .rules('add') method immediately after creating the element...

    var filenumber = 1;
    $("#AddFile").click(function () { //User clicks button #AddFile
    
        // create the new input element
        $('
  • Remove
  • ').prependTo("#FileUploader"); // declare the rule on this newly created input field $('#FileUpload' + filenumber).rules('add', { required: true, // <- with this you would not need 'required' attribute on input accept: "image/jpeg, image/pjpeg" }); filenumber++; // increment counter for next time return false; });
    • You'll still need to use .validate() to initialize the plugin within a DOM ready handler.

    • You'll still need to declare rules for your static elements using .validate(). Whatever input elements that are part of the form when the page loads... declare their rules within .validate().

    • You don't need to use .each(), when you're only targeting ONE element with the jQuery selector attached to .rules().

    • You don't need the required attribute on your input element when you're declaring the required rule using .validate() or .rules('add'). For whatever reason, if you still want the HTML5 attribute, at least use a proper format like required="required".

    Working DEMO: http://jsfiddle.net/8dAU8/5/

提交回复
热议问题