0

我陷入了这种循环的情况,当它被触发时我阻止它循环

这是我的代码

 $("#shiftForm").on('submit',
    function gvDDLValidation() {
        var gvDetDDLs = $('#positionShiftGrid').find("input[name=shiftDay]");
        $.each(gvDetDDLs, function () {
            var duplicateExists = false;
            var ddlShift = $("#ddlShiftDay option:selected").text();
            var currVal = $(this).val();
            gvDetDDLs.not(this).each(function (e) {  <--- where i trigger error
                e.preventDefault(); <--- where i trigger error
                if (ddlShift == currVal) {
                    duplicateExists = true;
                    alert("Duplicate entry is not allowed");
                    $(this).focus();
                    return false;
                }

            });
        });
        return true;
    });

谢谢 !

4

3 回答 3

0

稍微改写:

$("#shiftForm").on('submit', function(e) // "e" here represents the "submit" event, is that what you want?
    {
        var gvDetDDLs = $('#positionShiftGrid').find("input[name=shiftDay]");
        $.each(gvDetDDLs, function ()
            {
                var duplicateExists = false;
                // are you sure you don't want ".val()" on the next line?
                var ddlShift = $("#ddlShiftDay option:selected").text();
                // "$(this)" refers to the current "gvDetDDLs" from the "$.each" statement, is this what you want?
                var currVal = $(this).val();
                // what is "this" supposed to represent on the next line?
                gvDetDDLs.not(this).each(function ()
                    {
                        e.preventDefault();
                        // maybe "e.stopPropagation();" would be better?
                        if (ddlShift == currVal) {
                            duplicateExists = true;
                            alert("Duplicate entry is not allowed");
                            $(this).focus(); // focus where, exactly?
                            return false;
                        }
                    }
                );
            }
        );
        return true;
    }
);
于 2013-09-18T06:13:03.630 回答
0

您没有使用正确的e参数来阻止表单提交。这应该在提交处理程序上完成。您的提交处理程序的语法也不正确。它应该是一个匿名函数,您不需要从中返回 true 或 false:

$("#shiftForm").on('submit', function(e) {
    var gvDetDDLs = $('#positionShiftGrid').find("input[name=shiftDay]");
    $.each(gvDetDDLs, function () {
        var duplicateExists = false;
        var ddlShift = $("#ddlShiftDay option:selected").text();
        var currVal = $(this).val();
        gvDetDDLs.not(this).each(function () {
            e.preventDefault();
            if (ddlShift == currVal) {
                duplicateExists = true;
                alert("Duplicate entry is not allowed");
                $(this).focus();
            }
        });
    });
});
于 2013-09-18T06:13:14.347 回答
0

您可以运行我的演示代码并查看错误的含义。

当您遍历一个数组并期望调用一个方法时,您首先需要验证它是否存在。

var arr = [0, {test: function(){}}];

// the error - Uncaught TypeError: Object 0 has no method 'test'
//arr.forEach(function(e) {
//  e.test();
//});

    // the right way
arr.forEach(function(e) {
    if (e && e.test) {
        e.test();
    } else {
        console.log("no valid element or no support for method 'test'");
    }
});
于 2013-09-18T06:14:10.763 回答