ReferenceError:函数未定义 - 但它是

时间:2013-07-11 16:54:49

标签: javascript jquery

我有这个javacsript函数,它一直工作正常,但后来我添加了一些代码,除非我注释掉那段代码,它给出了错误:

ReferenceError: updateListingForm is not defined

完整的代码是:

function updateListingForm(type) {

    if (jQuery('#voucher_value').length){
        var voucher_value = jQuery('#voucher_value').val();
    } else {
        var voucher_value = null;
    }

    if (jQuery('#category_id')val().length > 0 && isNaN(jQuery('#category_id').val()) == false) {
        var category_id = jQuery('#category_id').val();
    } else {
        var category_id = 0;
    }

    var loadUrl    = relative_path + 'ajax_files/change_listing_type.php'; 
    var dataObject = { type: type, category_id: category_id, voucher: voucher_value }

    getAjaxData(loadUrl, dataObject, 'GET', 'json')

        .done(function(response) {

            console.log(response);

            // Add/remove the listing field stuff
            jQuery('#listing_type').html(response.listing_type);

            // Input addl cat fee
            jQuery('#addl_cat_fee').html(response.addl_cat_fee);

        })

        .fail(function() {
            alert('There seems to have been a problem changing the listing type; please contact us if this continues to occur.');
        });

    // End    

}

如果我注释掉下面的代码,它可以正常工作:

if (jQuery('#category_id')val().length > 0 && isNaN(jQuery('#category_id').val()) == false) {
    var category_id = jQuery('#category_id').val();
} else {
    var category_id = 0;
}

然而,为了让它工作正常,我显然需要添加这一行,如果注释掉上面的内容:

var category_id = 0;

我的代码有什么问题导致它出错?

2 个答案:

答案 0 :(得分:6)

jQuery('#category_id')val()是语法错误。你想要jQuery('#category_id').val()

您会看到错误updateListingForm is not defined,因为语法错误会暂停解析器并中止函数的创建。

尝试使用该功能时,会发生功能未定义的错误。从解析器尝试创建函数时,您还应该在控制台中看到之前的错误:Chrome中的Uncaught SyntaxError: Unexpected identifier,Firefox中的SyntaxError: missing ) after condition ,或IE中的Expected ')'。该错误应该指向问题的确切行。

答案 1 :(得分:3)

if语句的第一个条件有错误。

jQuery('#category_id').val().length
                      ^------Missing this
相关问题