js / jquery - event.preventdefault似乎在firefox中不起作用

时间:2012-01-19 05:09:04

标签: javascript jquery firefox

我有一些由其他人编写的代码,如果邮政编码格式不正确并且停止提交页面,则应该打开弹出窗口。它在IE和Chrome中正常工作。但在Firefox中我得到弹出窗口,单击确定,然后页面提交。有人可以查看代码,让我知道错误做了什么?代码粘贴在此消息的末尾。

谢谢

<script type="text/javascript">
$(init);

function init() {
    $('form').validate({
        error_messages: {

        },
        failure: function (errors) {
            //alert(errors);
            $(".errMsg").show();
            return false;
        },
        success: function () {
            //alert('passed');
            //return true;
        }
    });

    $('#<%= btnAdd.ClientID %>').click(function (event) {
        if (beginZipValidation()) {
            //event.preventDefault();

            return;
        }
    });
}

function clearText(mybox, mymsg) {
    if (document.forms['form1'].elements[mybox].value == mymsg) {
        document.forms['form1'].elements[mybox].value = '';
        document.forms['form1'].elements[mybox].style.color = '#000000';
    }

}

function resetText(mybox, mymsg) {
    if (document.forms['form1'].elements[mybox].value == '') {
        document.forms['form1'].elements[mybox].value = mymsg;
        document.forms['form1'].elements[mybox].style.color = '#C0C0C0';
    }
}



function beginZipValidation() {
    //alert('begin validation');
    var zip = $('#<%= Zip.ClientID %>').val().replace(/ /g, '').toUpperCase();
    var cID = $("#<%= ddlCountry.ClientID %> option:selected").val();
    if (!zipCodeValidation(true, cID, zip)) {
        return false;
    }

    //alert('true');
    return true;
}

function zipCodeValidation(shouldValidateEmpty, countryID, zc) {
    //alert('zipCodeValidation');
    if (zc == '' || zc == 'POSTALCODE') {
        if (!shouldValidateEmpty) {
            return true;
        }
    }
    else {
        switch (countryID) {
            case '226':
                if (/^\d{5}(-\d{4})?$/.test(zc))
                    return true;
                break;
            case '38':
                if (/^([ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ])\ ?([0-9][ABCEGHJKLMNPRSTVWXYZ][0-9])$/.test(zc))
                    return true;
                break;
            case '225':
                if (/^(GIR 0AA|[A-PR-UWYZ]([0-9][0-9A-HJKPS-UW]?|[A-HK-Y][0-9][0-9ABEHMNPRV-Y]?)[0-9][ABD-HJLNP-UW-Z]{2})$/.test(zc))
                    return true;
                break;
            case '13':
                if (/^(((2|8|9)\d{2})|((02|08|09)\d{2})|([1-9]\d{3}))$/.test(zc))
                    return true;
                break;
        }
    }

    alert('The postal code provided does not fit the format for the selected country. Please adjust and try again.');
    if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }
    event.preventDefault();

    return false;

}

1 个答案:

答案 0 :(得分:0)

可能就像选择器错误一样简单:

尝试更改此内容:

 $('#<%= btnAdd.ClientID %>').click(function (event) {
        if (beginZipValidation()) {
            //event.preventDefault();

            return;
        }
    });

反而有点像:

var myButtonId = "<%= btnAdd.ClientID %>"; // Now you know what this evaluates to client-side
var btnSelector = "#" + myButtonId;
$(btnSelector).click(function (event) {
        if (beginZipValidation()) {
            //event.preventDefault();

            return;
        }
    });

您是否正在使用Firebug或Chrome Developer工具等标准工具进行调试?如果是这样,请在那里抛出几个console.log("myButtonId: " + myButtonId);语句!