“this[0] is undefined” message using jQuery validate plugin

前端 未结 4 2056
误落风尘
误落风尘 2021-02-13 19:09

I\'ve started to use the jQuery validate plugin. I was having a few issues with the display of the error messages and wanted to create a test page where I could experiment with

相关标签:
4条回答
  • 2021-02-13 19:25

    Wrap your code in a document ready function such as:

    $(function()
    {
    
        $('#myform').validate({
                        rules: {
                            thisval: "required"
                        }
        });
        console.info($('#myform').valid());
    
    });
    
    0 讨论(0)
  • 2021-02-13 19:27

    Im my case it happens when you miss to specify the id in form tag where you did specified in validate method.

    0 讨论(0)
  • 2021-02-13 19:33

    Same happened here. For me it was a typo in my code:

    $(document).ready(function(){
        //START of validate
    $('#reply').validate({
        rules:{
            message:{
                required: true,
                },  
            },
        submitHandler: function(form) {
            var data = $("#reply").serialize();
            $.ajax({
                type:"POST",
                url:"ajax/scripts/msg_crt.php",
                data:data,
                success:function(data){
                    alert("Loaded");
                    }
                });
            }
        });
        //END of validate
      });
    
    $(document).on('click','#send', function(){
        if($('#replay').valid()){// <--- Here is my selector typo
            $("#replay").submit(); // <--- Here is my selector typo
            }
            return false;
        });
    
    0 讨论(0)
  • 2021-02-13 19:44

    Your #myform form is not loaded when the script is executed, so $('#myform') doesn't match anything. Wrap your script in a ready event handler.

    <script type="text/javascript">
       $(function() { // <-- add this
          $('#myform').validate({
                        rules: {
                            thisval: "required"
                        }
          });
          console.info($('#myform').valid());
       });
    </script>
    
    0 讨论(0)
提交回复
热议问题