亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在下拉列表取消選擇時更改對象數組中的值

在下拉列表取消選擇時更改對象數組中的值

慕容森 2022-08-18 15:44:22
我想根據下拉列表中的選擇來更改對象數組中的值。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
查看完整描述

1 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

我用它解決了這個問題$.inArray


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'}

     ]

console.log(arr);


let yo;

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

   yo = [];

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

        yo.push(this.value);

   });

   

 console.log(yo);


  arr.forEach(arr_opts => {

        if ($.inArray(arr_opts.name, yo) !== -1) {

        arr_opts.in_order = true;

    } else {

        arr_opts.in_order = false;

    }

  });

  console.log(arr);

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<select id='filter' multiple>

  <option value='one' selected>one</option>

  <option value='two'>two</option>

  <option value='three'>three</option>

  <option value='four'>four</option>

</select>


查看完整回答
反對 回復 2022-08-18
  • 1 回答
  • 0 關注
  • 72 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號