简体   繁体   English

尝试执行ajax调用时,TypeError:elem.nodeName未定义

[英]TypeError: elem.nodeName is undefined when trying to do an ajax call

Good morning everyone. 大家,早安。

I know this may have been asked before, but I'm finding it difficult to get a solution specific to my circumstances so I thought I'd ask here. 我知道之前可能已经提出这个问题,但我发现很难找到一个特定于我的情况的解决方案,所以我想我会问这里。

I am working on a site which has an account section for customers to add Pets to their inventory. 我正在一个网站上工作,该网站有一个帐户部分供客户将宠物添加到他们的库存中。 The form itself has a "species" drop down which in turn triggers a change event on the "breeds" drop down in order to populate it with breed specific to the chosen species. 形式本身具有“物种”下降,这反过来触发“品种”下降的变化事件,以便用所选物种特有的品种填充它。

However, in the event of the user submitting the form prematurely without having selected a breed, an error will show but the breeds drop down is now un-populated as the trigger is on the change of the species drop down. 但是,如果用户在没有选择品种的情况下提前提交表格,则会显示错误,但品种下降现在是未填充的,因为触发器是物种下降的变化。 So, to fix this I added a clause in the javascript to detect whether the species drop down has a value on load which in turn will trigger the function that populates the breed box. 因此,为了解决这个问题,我在javascript中添加了一个子句,用于检测物种下拉是否具有加载值,从而触发填充品种框的功能。 However, I get the error above when the page is reloaded and the box is not populated. 但是,当重新加载页面并且未填充该框时,我收到上述错误。

Here is the jQuery: 这是jQuery:

jQuery('#species').change(function () {
    var species_id = $(this).val();
    loadBreeds(species_id);
});

if (jQuery('#species').val() != "") {
    var species_id = $(this).val();
    loadBreeds(species_id);
}

function loadBreeds(species_id) {
    console.log('loadBreeds is running');
    jQuery.ajax({
        url: "http://dev.mediaorb.co.uk/hiltonherbs.com/index.php?route=account/breeds",
        type: "post",
        data: "species_id=" + species_id + "&action=populateBreeds",
        success: function (data) {
            console.log('\nSuccess.');
            jQuery('#breed option').remove();
            jQuery('#breed').append('<option value="">-- Please select --</option>');
            if (!data) {
                jQuery('#breed').append('<option value="" disabled="disabled">No breeds found for selected species.</option>');
            } else {
                jQuery('#breed').append(data);
            }
        }
    });
}

And here is part of the form HTML: 这是HTML表单的一部分:

    <div class="form-group">
    <label for="species" class="required">Species</label>
    <select class="form-control" id="species" name="species">
        <option value="" selected="selected">-- Please select --</option>
        <option value="1">Dog</option>
        <option value="2" selected="selected">Cat</option>
        <option value="3">Bird</option>
        <option value="4">Horse</option>
    </select>
</div>
<div class="form-group">
    <label for="breed" class="required">Breed</label>
    <select name="breed" id="breed" class="form-control">
        <option selected="selected" value="">-- Please choose a species first --</option>
</div>

I have created a jsfiddle here to illustrate this functionality: http://jsfiddle.net/1961bgay/ 我在这里创建了一个jsfiddle来说明这个功能: http//jsfiddle.net/1961bgay/

Ideally you will need the console open (or firebug on Firefox) to see when the error occurs. 理想情况下,您需要打开控制台(或Firefox上的firebug)以查看错误发生的时间。

I think it may be relating potentially to the fact I have duplicated the jQuery selectors, but this is required in order to check and to add the trigger...any help appreciated, thank you! 我认为它可能与我重复jQuery选择器的事实有关,但这是检查和添加触发器所必需的...任何帮助表示感谢,谢谢!

Michael 迈克尔

You just have a small JS context/scoping error: 你只有一个小的JS上下文/作用域错误:

Change line 9: 换行9:

From this: var species_id = $(this).val(); 由此var species_id = $(this).val();var species_id = $(this).val();

To this: var species_id = jQuery('#species').val() 对此: var species_id = jQuery('#species').val()

Here is your working jsFiddle: http://jsfiddle.net/xgkefvbq/ 这是你的工作jsFiddle: http//jsfiddle.net/xgkefvbq/

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM