JQuery逻辑没有应用于动态创建的html

时间:2016-03-14 21:56:44

标签: javascript jquery html

我编写了以下JavaScript / JQuery代码。从HTML下拉菜单中选择选项时会触发此代码。它将选定的值与已添加到HTML表中的值进行比较:

bar z

如果我从表格中已存在的下拉列表中选择一个选项,则会显示警报(它应该这样做)。然后,如果我选择一个不在HTML表中的选项,它会将它添加到表中(这是正确的)。但是,如果我选择新添加的选项,则会触发警报,但会再次将其添加到表中。它只应显示警报,而不是第二次添加。

为了防止这种情况,我在代码中缺少什么?

修改
基于缺少MCVE,这是下拉HTML:

$('#addonSelectOptions').on('change', function(e){
    e.preventDefault();

    var selectedAddon = $('#addonSelectOptions option:selected').val();
    var selectedText = $('#addonSelectOptions option:selected').text();
    var currentAddons = $('#acg_addons').val();

    if(selectedAddon == '0') {
        $('#availableAddOns > tbody:last').empty();
        $('#availableAddOns > tbody:last').append('<tr id="noaddonShown"><td>No add-ons</td></tr>');
        $('#acg_addons').val(' ');
    } else {

        var addonStrings = currentAddons.toString();
        var addonCheck = 'addOn-'+selectedAddon;

        $(".selectedAddonTableTR").each(function() {
            var rowID = $(this).attr('id');

            if(rowID == addonCheck) {
                alert('You have already added this Add On to this Game');
                return false;
            } else {
                $('#noaddonShown').empty();
                $('#availableAddOns > tbody:last').append('<tr id="addOn-'+selectedAddon+'" class="selectedAddonTableTR"><td>'+selectedText+'</td></tr>');
                var addons = $('#acg_addons').val();
                $('#acg_addons').val(addons+''+selectedAddon+',');
            }

        });

    }

});

这也是使用的HTML表格:

<select id="addonSelectOptions">
    <option value="0">--No Addon--</option>
    <option value="2">Add On 1</option>
    <option value="1">Add On 2</option>
</select>

1 个答案:

答案 0 :(得分:0)

这应该让你走上正确的道路。你没有做好解释问题的工作,但我认为这会让你朝着正确的方向前进。阅读我的评论,如果您需要更详细的解释,请告诉我。

$('#addonSelectOptions').on('change', function(e){
    e.preventDefault();

    var select = $(this), //Don't use var declaration more than once
        selectedAddon = $(select).val(),
        selectedAddonIdString = 'addOn-' + selectedAddon,
        selectedText = $(select).find('option:selected').text(),
        availableAddOns = $('#availableAddOns'), // Cache your DOM elemtns, don't select them more than once
        currentAddonArr = []; //Use an array to store multiple values... I don't know what you were trying to do with addons = $('#acg_addons').val();

    if(selectedAddon == '0') {
        $(availableAddOns).find('> tbody:last').empty();
        //You didn't prove any HTML, but you can't dynamically add an HTML element here, but then reference it below since it may not exist
        //If this part of the if statement never fires, the #noaddonShown element will not exist
        $(availableAddOns).find('> tbody:last').append('<tr id="noaddonShown"><td>No add-ons</td></tr>');        // Rework this
    } else {

        if($.inArray(selectedAddonIdString, currentAddonArr) === -1) {
            //You don't need to run empty funtions on an element that will simply show the same information over and over again if a condition is met.
            //Instead use $('#noaddonShown').hide(); $('#noaddonShown').show(); to toggle the element
            $('#noaddonShown').empty(); // rework this
            $(availableAddOns).find('> tbody:last').append('<tr id="addOn-'+selectedAddon+'" class="selectedAddonTableTR"><td>'+selectedText+'</td></tr>');
            //var addons = $('#acg_addons').val(); not needed
            //$('#acg_addons').val(addons+''+selectedAddon+',');

        } else {
            alert('You have already added this Add On to this Game');
            return false;
        }

    }

});