【问题标题】:How to get multiple select box values and texts together using jQuery?如何使用 jQuery 将多个选择框值和文本放在一起?
【发布时间】:2020-08-04 01:24:38
【问题描述】:

我有一个选择框,用户可以选择多种颜色 所以我想用下面的代码获取所选选项的值和文本我只是获取文本或只获取值,但我想要两者

  selected_colors = $('.selectpicker option:selected')
                .toArray().map(item => item.text);
                console.log(selected_colors);

它给了我这个

(2) ["بنفش", "نارنجی روشن"]0: "بنفش"1: "نارنجی روشن"length: 2__proto__: Array(0)

btw "نارنجی روشن" 和 ... 是颜色名称

【问题讨论】:

    标签: javascript jquery ajax ecmascript-6 ecmascript-2016


    【解决方案1】:

    在下面试试这个:

    selected_colors = $('.selectpicker option:selected').toArray().map(item => ({'text':item.text, 'value':item.value}));
    
    console.log(selected_colors);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      相关资源
      最近更新 更多