在下拉列表取消选择时更改对象数组中的值

我想根据下拉列表中的选择来更改对象数组中的值。


arr= [

        {name: 'one', in_order: true, other_key:'ga'},

        {name: 'one', in_order: true, other_key:'gb'},

        {name: 'one', in_order: true, other_key:'gc'},

        {name: 'two', in_order: false, other_key:'gd'},

        {name: 'two', in_order: false, other_key:'ge'},

        {name: 'two', in_order: false, other_key:'gf'},

        {name: 'three', in_order: false, other_key:'gg'},

        {name: 'three', in_order: false, other_key:'gh'},

        {name: 'three', in_order: false, other_key:'gi'},

        {name: 'four', in_order: false, other_key:'gj'},

        {name: 'four', in_order: false, other_key:'gk'},

        {name: 'four', in_order: false, other_key:'gl'}

     ]

我这样做了:


$(`#filter`).on('change', function() {

   yo = [];

   $('option:selected', this).each(function() {

        yo.push(this.value);

   });


  arr.forEach(arr_opts => {

            yo.forEach(opt => {

                if (arr_opts.name == opt) {

                    arr_opts.in_order = true;

                }

            });

        });

        console.log(arr);

});

我这样做,我得到我的结果。但是当我在下拉列表中取消选择结果时,它不会变回false。例如,如果我检查和.它将数组中两者的 in 值更改为 。但是,如果我随后取消选择 ,则保持为 。onetwotruetwotwotrue


取消选择后,是否可以将所选值更改回 ?false


慕容森
浏览 81回答 1
1回答

BIG阳

我用它解决了这个问题$.inArrayarr= [&nbsp; &nbsp; &nbsp; &nbsp; {name: 'one', in_order: true, other_key:'ga'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'one', in_order: true, other_key:'gb'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'one', in_order: true, other_key:'gc'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'two', in_order: false, other_key:'gd'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'two', in_order: false, other_key:'ge'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'two', in_order: false, other_key:'gf'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'three', in_order: false, other_key:'gg'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'three', in_order: false, other_key:'gh'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'three', in_order: false, other_key:'gi'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'four', in_order: false, other_key:'gj'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'four', in_order: false, other_key:'gk'},&nbsp; &nbsp; &nbsp; &nbsp; {name: 'four', in_order: false, other_key:'gl'}&nbsp; &nbsp; &nbsp;]console.log(arr);let yo;$(`#filter`).on('change', function() {&nbsp; &nbsp;yo = [];&nbsp; &nbsp;$('option:selected', this).each(function() {&nbsp; &nbsp; &nbsp; &nbsp; yo.push(this.value);&nbsp; &nbsp;});&nbsp; &nbsp;&nbsp;console.log(yo);&nbsp; arr.forEach(arr_opts => {&nbsp; &nbsp; &nbsp; &nbsp; if ($.inArray(arr_opts.name, yo) !== -1) {&nbsp; &nbsp; &nbsp; &nbsp; arr_opts.in_order = true;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; arr_opts.in_order = false;&nbsp; &nbsp; }&nbsp; });&nbsp; console.log(arr);});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select id='filter' multiple>&nbsp; <option value='one' selected>one</option>&nbsp; <option value='two'>two</option>&nbsp; <option value='three'>three</option>&nbsp; <option value='four'>four</option></select>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript