jQuery 验证后使用数据属性进行验证

jQuery validation with data attributes after validation

我正在使用具有数据属性的 jQuery 验证规则,它工作正常,但一旦验证了必填字段,其余 jquery 代码将不起作用。所以它确实验证了 ItemID,但之后成功功能不起作用。

代码如下:

HTML

<input type="text" name="itemID" id="itemID" class="required" data-rule-required="true" data-msg-required="Please enter an item to search">

Jquery

<script>
$(document).ready(function()
{
    var validator = $("#Form").validate(
    {
        errorClass: 'validateError',
        errorContainer: ".EmphasisDanger",
        wrapper: "li"
    },
    success: function(){
       stItems = $('#itemID').val().replace(/(?:\r\n|\r|\n)/g, ',');
        document.Form.target='_parent';
        document.Form.action='/admin/system/index.cfm?JobIDs=' + stItems;
        document.Form.submit();
        });
});

你的结构没有意义。

它应该是这样的...注意逗号和大括号的位置...

$(document).ready(function() {

    var validator = $("#Form").validate({
        errorClass: 'validateError',
        errorContainer: ".EmphasisDanger",
        wrapper: "li",
        success: function() {
            .... 
        }
    });

});

根据 the docssuccess 回调函数,

If specified, the error label is displayed to show a valid element. If a String is given, it is added as a class to the label. If a Function is given, it is called with the label (as a jQuery object) and the validated input (as a DOM element). The label can be used to add a text like "ok!".

换句话说,success 函数不是您放置提交的位置。

也许,您打算使用 the submitHandler callback function...

$(document).ready(function() {

    var validator = $("#Form").validate({
        errorClass: 'validateError',
        errorContainer: ".EmphasisDanger",
        wrapper: "li",
        submitHandler: function(form) {
            ....
            // use the 'form' argument provided by the developer
            form.submit();
        }
    });

});