jQuery mobile form validation with no results

前端 未结 1 1450
忘掉有多难
忘掉有多难 2020-12-11 12:18

I am trying to learn jQuery form validations for jQuery Mobile. I keep coming across this tutorial, which I am trying to replicate but must be doing something wrong with the

1条回答
  •  無奈伤痛
    2020-12-11 12:40

    Typically, you'd initialize any jQuery plugin within a DOM ready event handler. This ensures the HTML is constructed before .validate() is called and initialized. As you can see, you're calling it up top before the HTML form exists. Putting it inside a ready event handler prevents the code from firing until after the page's HTML is fully constructed.

    Since you're using jQuery Mobile (it uses ajax to load pages), use a .on('pageinit') handler like this...

    $(document).on('pageinit', function() { // <-- ensures the DOM is ready
    
        $("#frmLogin").validate({
            // your rules & options
        });
    
    });
    

    Working DEMO: http://jsfiddle.net/5p9N5/

    From your browser, do a "view source" of this page to see how the scripts are properly included. http://jsfiddle.net/5p9N5/show/....

    
        
            
            jsFiddle demo
            
            
            
            
              
            
        
        
            
    .....

    For a non-jQuery-mobile webpage, you'd typically enclose your jQuery code within a .ready() handler like this...

    $(document).ready(function() { // <-- ensures the DOM is ready
    
        $("#frmLogin").validate({
            // your rules & options
        });
    
        // any other jQuery
    
    });
    

    0 讨论(0)
提交回复
热议问题