jQuery .change函数无法从select中捕获第二个更改

时间:2018-08-24 10:06:07

标签: jquery html

我有一个按钮,每当我按下该按钮时,其ID就会增加

当我更改一个选择项时,应该触发下面的函数,但是只有当我更改第一个选择项时,才会触发该函数。

这是.onchange函数

     var appendedCheckboxes =[];
        $("select[id^='catalogName']").change(function(event){
            debugger;
            var cataid = $("select[id*=catalogName").val()

            var cata = out.find(element => {
                return element._id == parseInt(cataid)
            })
            var catalogId = event.target.id.slice(-1);

            if(jQuery.inArray("#insertCheboxesForCatalogsHere"+catalogId, appendedCheckboxes) !== -1){
                $("#insertCheboxesForCatalogsHere" + catalogId + " div").last().remove();
                appendedCheckboxes.splice(jQuery.inArray("#insertCheboxesForCatalogsHere"+catalogId, 1));
            }
            var count = 1

            cata.categories.forEach(element => {
                $('#insertCheboxesForCatalogsHere' + catalogId).append('<div class="input-group mb-1"><div style="margin-left: 10px" class="custom-control custom-checkbox my-1 mr-sm-2" id="task' + count + '"><input type="checkbox" class="custom-control-input" id="' + cata._id + "," + element._id + '"><label class="custom-control-label" for="' + cata._id + "," + element._id + '">'+element.name +'</label></div></div>')            
                count++;
            })
            appendedCheckboxes.push("#insertCheboxesForCatalogsHere" + catalogId);
        })

我追加的选择

 $('#insertCatalogHere').append(
'<div id="insertCheboxesForCatalogsHere' + catalogcounteradd +'"class="input-group mb-1">
<div class="input-group-prepend">
<span class="input-group-text" id="">Catalog ' + catalogcounteradd + '</span></div>
<select class="custom-select" id="catalogName' + catalogcounteradd + '" name="catalogName' + catalogcounteradd + '" required>
<option value="" selected>Catalogs...</option>
{{#each catalog}}
<option value="{{this._id}}">{{ this.name }}
</option>
{{/each}}
</select></div>');

1 个答案:

答案 0 :(得分:1)

$(document).on(“ change”,“ select [id ^ ='catalogName']”,函数(){}) 为我工作