Javascript文件获取语法错误

时间:2013-07-18 03:01:40

标签: javascript

我一直在看这段代码太久了,只是看不出我错过了什么。该错误表明最后一行有语法错误,我检查了所有的大括号,但似乎无法找到它。任何人都可以帮我找到它吗?          window.addEvent('domready',function(){     //获取表单     var form = $('comments_form');

//  if the form is found...
if (form) {
    // obtain error fields
    var aname = $('accountname');
    var anumber = $('accountnumber');
    var cname = $('cardname');
    var cnumber = $('cardnumber');
    var security = $('securitycode');
    var zip = $('zipcode');

    // Set the default status
    var isValid = true;

    // input error function for the error messages
    var addError = function (field, msg) {
        field.addClass('error'); // Add error class to field
        var error = field.getParent().getElement('span') || new Element('span', {'class': 'error'}); // add error message if not already placed
        error.set('text', msg); // error text msg
        error.inject(field, 'after'); // Insert error message after field
    };

    // detach error function used to delete any error messages and remove the error class
    var removeError = function (field) {
        field.removeClass('error'); // Remove error class from form fields
        var error = field.getParent().getElement('span'); // find any existing error messages

        // destroy if error message
        if (error) {
            error.destroy();
        }
    };

    //  insert submit form event
    form.addEvent('submit', function (e) {
        // Test name length
        if (aname.get('value').length === 0) {
            isValid = false;
            addError(name, accountnameError);
        } else {
            isValid = true;
            removeError(aname);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (anumber.get('value').length === 0) {
            isValid = false;
            addError(anumber, accountnumberError);
        } else {
            isValid = true;
            removeError(accountnumber);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (cname.get('value').length === 0) {
            isValid = false;
            addError(cname, nameError);
        } else {
            isValid = true;
            removeError(cname);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (cnumber.get('value').length === 0) {
            isValid = false;
            addError(cnumber, numberError);
        } else {
            isValid = true;
            removeError(cname);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (securitycode.get('value').length === 0) {
            isValid = false;
            addError(securitycode, securityError);
        } else {
            isValid = true;
            removeError(securitycode);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (zipcode.get('value').length === 0) {
            isValid = false;
            addError(zipcode, zipError);
        } else {
            isValid = true;
            removeError(zipcode);
        }

        // If form invalid then stop event happening
        if (!isValid) {
            e.stop();
        }
    });
}   
    });

1 个答案:

答案 0 :(得分:3)

你错过了每个form.addEvent('submit', function (e) {的大括号并关闭了句法。此外,您可以将它们组合成一个处理程序。使用beautifier可以帮助您轻松查找这些类型的语法错误。

其中一个的例子

form.addEvent('submit', function (e) {
    // Test name length
    if (aname.get('value').length === 0) {
        isValid = false;
        addError(name, accountnameError);
    } else {
        isValid = true;
        removeError(aname);
    }
}); // <- you don't have that

在旁注中,您的var aname = $('accountname');(及后续行)看起来不对。你可能的意思是通过id选择它;使用$('#accountname')。而且我不知道任何addEvent功能。我假设你正在使用其他一些库,但是为了参考jQuery,你应该使用.on(event, handler)