jQuery 文本区域验证无效

jQuery Textarea-validation doesn't work

我想验证带有文本区域的表单。到目前为止,一切都很好。但是当我尝试使用 jQuery validate plugin nothing happens. The tooltip doesn't show up. I'm using 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) {

}
});

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

您只能在 rules 对象中使用 name

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.