对象0没有方法'preventDefault'

时间:2013-09-18 06:01:48

标签: javascript jquery

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

这是我的代码

 $("#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;
    });

谢谢!

3 个答案:

答案 0 :(得分: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;
    }
);

答案 1 :(得分: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();
            }
        });
    });
});

答案 2 :(得分: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'");
    }
});