[英]Selecting values from multiple select option box - Jquery
我正在嘗試在多個 select 選項框中輸入 select 值。 當我在下面運行當前代碼時,選項框中沒有選擇任何項目,但是當我執行 parseInt(value) 時,它只選擇第一個選項。
當我 console.log(selected) 時,它返回 4,5。 如何將 4,5 插入 $("#edit-items") 以使其 select 兩個值都在選項框中。
PS:對不起我的英語
JS
var selected = $(this).attr('data-items'); returns 4,5
items = parseInt(selected)
$("#edit-items").val([items]);
parseInt()
只會從4,5
解析第一個 integer 。 如果要將所有整數作為數組獲取,請拆分字符串並在每個數組元素上調用parseInt()
。
items = selected.split(',').map(str => parseInt(str));
$("#edit-items").val(items);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.