我正在嘗試在多選選項框中選擇值。當我在下面運行當前代碼時,選項框中沒有選擇任何專案,但是當我執行 parseInt(value) 時,它只選擇第一個選項。
當我 console.log(selected) 時,它回傳 4,5。如何將 4,5 插入 $("#edit-items") 以使其選擇選項框中的兩個值。
PS:對不起我的英語
JS
var selected = $(this).attr('data-items'); returns 4,5
items = parseInt(selected)
$("#edit-items").val([items]);
uj5u.com熱心網友回復:
parseInt()只會從 決議第一個整數4,5。如果要將所有整數作為陣列獲取,請拆分字串并呼叫parseInt()每個陣列元素。
items = selected.split(',').map(str => parseInt(str));
$("#edit-items").val(items);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/409515.html
標籤:
