ReferenceError:$未定义$(" form")。validate({

时间:2016-02-18 05:47:59

标签: jquery error-handling referenceerror

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery.validate/1.11.1/jquery.validate.js" type="text/javascript"></script>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>


<script type="text/javascript">

$("form").validate({
    showErrors: function(errorMap, errorList) {
        // Clean up any tooltips for valid elements
        $.each(this.validElements(), function (index, element) {
            var $element = $(element);
            $element.data("title", "") // Clear the title - there is no error associated anymore
                    .removeClass("error")
                    .tooltip("destroy");
        });

        // Create new tooltips for invalid elements
        $.each(errorList, function (index, error) {
            var $element = $(error.element);
            $element.tooltip("destroy") // Destroy any pre-existing tooltip so we can repopulate with new tooltip content
                    .data("title", error.message)
                    .addClass("error")
                    .tooltip({ placement: 'right'}); // Create a new tooltip based on the error messsage we just set in the title
        });
    },

});

</script>

我添加了这些标题。但是我得到错误referenceError $ not defined.I尝试了各种可能的方法。我仍然得到这个错误该怎么办?

1 个答案:

答案 0 :(得分:-1)

我建议两件事,

  • 添加文档准备
  • 在表单中添加ID,并将$("form")的选择器更改为$("form#my_form")

      $(&#34; document&#34;)。ready(function(){     $(&#34;#形式myForm的&#34)。验证({
      showErrors: function(errorMap, errorList) {
        // Clean up any tooltips for valid elements
        $.each(this.validElements(), function(index, element) {
          var $element = $(element);
          $element.data("title", "") // Clear the title - there is no error associated anymore
            .removeClass("error")
            .tooltip("destroy");
        });
        // Create new tooltips for invalid elements
        $.each(errorList, function(index, error) {
          var $element = $(error.element);
          $element.tooltip("destroy") // Destroy any pre-existing tooltip so we can repopulate with new tooltip content
            .data("title", error.message)
            .addClass("error")
            .tooltip({
              placement: 'right'
            }); // Create a new tooltip based on the error messsage we just set in the title
        });
      },
    
    });
    
      });

这应该适用。

由于