编程语言
首页 > 编程语言> > javascript-使用ParsleyJS验证并使用Twitter工具提示显示错误

javascript-使用ParsleyJS验证并使用Twitter工具提示显示错误

作者:互联网

我想用欧芹js验证表单,并使用twitter的bootstrap工具提示显示错误(如果有).

我读了link,了解有关与欧芹的twitter引导集成,以及有关stackoverflow question的EventListeners.但是,我仍然无法显示错误消息.

我就是这样实现的

... 
<input id="id_email" name="email" required=True parsley-type="email">
<button type="submit" onclick="javascript:$('#id_email').parsley(parsleyOptions); ">Next</button>
...
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>   
<script type="text/javascript" src="{% static "js/bootstrap.min.js" %}"></script>
<script type="text/javascript" src="{% static "js/parsley/parsley.js"%}"></script>
...
    <script>

    // parsley option
    var parsleyOptions = {
      // Sets success and error class to Bootstrap class names
      successClass: 'has-success',
      errorClass: 'has-error',

      // Bootsrap needs success/error class to be set on parent element
      errors: {
        classHandler: function(el) {
          return el.parent();
        },
        // Set these to empty to make sure the default Parsley elements are not rendered
        errorsWrapper: '',
        errorElem: ''
      },
      listeners: {
        // Show a tooltip when a validation error occurs
        onFieldError: function (elem, constraints, parsleyField) {
          elem.tooltip({
            animation: false,
            container: 'body',
            placement: 'top',
            title: elem.data('error-message')
          });
        },
        // Hide validation tooltip if field is validated
        onFieldSuccess: function(elem, constraints, parsleyField) {
          elem.tooltip('destroy');
        }
      }
    };

    </script>
...

不会在表单字段中出现错误时触发工具提示(即使出现空白字段也不会引发错误).如何获得欧芹触发工具提示的工作?

旁注:

>关于“ parsleyOptions”的代码取自here
>所有js文件均可访问
>在输入字段上的静态工具提示有效
>当在div中显示错误时,parsley js可以工作

解决方法:

对于那些对使用欧芹2.x实现此方法感兴趣的人:

<form method="post" id="myForm">
    <input id="id_email" name="email" class="required" data-parsley-type="email">
    <button type="submit">Next</button>
</form>

<script type="text/javascript">
    $(document).ready(function() {  
        // instanciate parsley and set the container 
        // as the element title without a wrapper
        $("#myForm").parsley({
            errorsContainer: function (ParsleyField) {
                return ParsleyField.$element.attr("title");
            },
            errorsWrapper: false
        });

        // when there is an error, display the tooltip with the error message
        $.listen('parsley:field:error', function(fieldInstance) {
            var messages = ParsleyUI.getErrorsMessages(fieldInstance);
            fieldInstance.$element.tooltip('destroy');
            fieldInstance.$element.tooltip({
                animation: false,
                container: 'body',
                placement: 'top',
                title: messages
            });
        });

        // destroy tooltip when field is valid
        $.listen('parsley:field:success', function(fieldInstance) {     
            fieldInstance.$element.tooltip('destroy');
        });
    });
</script>

我正在使用Bootstrap 3.2.0和Parsley.js 2.0.2.我用这个创建了一个小提琴:http://jsfiddle.net/C96ab/3/

标签:parsley-js,twitter-bootstrap,listener,javascript,jquery
来源: https://codeday.me/bug/20191029/1963420.html