jQuery Textarea验证不起作用

时间:2017-05-28 10:39:25

标签: javascript jquery jquery-validate tooltipster

我想用textarea验证表单。到现在为止还挺好。但是当我尝试用jQuery validate插件初始化textarea时没有任何反应。工具提示不会显示。我正在使用tooltipster和jQuery-validate插件。

在控制台中,没有出现JavaScript错误。

这是表格:

<form name="reportForm" id="reportForm" method="POST">
<div class="form_alt_design">
    <label for="the_report"></label>
    <textarea name="report" cols="60" rows="4" maxlength="2000" id="the_report" class="report_textbox"></textarea>
</div>
<div id="report_button_container" style="text-align:right;">
    <input class="button_large2" type="submit" name="Submit" id="report_submit" value="<?php echo GAME_SUBMIT_REPORT;?>" onclick="submitReport(<?php echo $id.",'".$type;?>')" />
    <input class="button_large2" type="submit" name="Submit" id="report_submit" value="<?php echo GAME_SUBMIT_REPORT;?>" onclick="" />

    <input type="hidden" name="token" id="token" value="<?php echo $reportToken; ?>" />
</div>

JavaScript代码:     

$(document).ready(function() {
$('#reportForm textarea').tooltipster({
    trigger: 'custom',
    animation: 'fade',
    animationDuration: 250,
    delay: 0,
    onlyOne: true,
    position: 'right',
    theme: ['tooltipster-noir', 'tooltipster-noir-customized']
});

// initialize validate plugin on the form
$('#reportForm').validate({

    errorPlacement: function (error, element) {

        var ele = $(element),
        err = $(error),
        msg = err.text();
        if (msg != null && msg !== '') {
                ele.tooltipster('content', msg);
                ele.tooltipster('open');
    }
    }

    },
    success: function (label, element) {
        $(element).tooltipster('hide');        },
    unhighlight: function(element, errorClass, validClass) {
        $(element).removeClass(errorClass).addClass(validClass).tooltipster('close');
    },
    rules: {
        the_report: {
          required:true
        }
    },
    submitHandler: function (form) {

}
});

1 个答案:

答案 0 :(得分:1)

您已将required规则分配给the_report,但您的textarea包含name="report"

您只能使用name对象中的rules

rules: {
    report: { //  <- MUST match the NAME, not the ID
        required: true
    }
},

您从未说过哪个版本的ToolTipster,而是here is a working jsFiddle demo using v4.0

REFERENCE: Proper jQuery Validate integration for ToolTipster versions 2, 3, and 4