gpt4 book ai didi

javascript - 如何将值设置为在 select2 中显示而不是文本

转载 作者:行者123 更新时间:2023-11-29 21:15:41 25 4
gpt4 key购买 nike

$('select').select2({width:"300"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<select id="convertIt" multiple>
<option value="A">Australia</option>
<option value="U">US </option>
<option value="J">Japan</option>
<option value="E">Europe</option>
<option value="C">Canada</option>
</select>

我也试过 id 但没有成功

$('#convertIt').select2({
placeholder: "Select an country"
}).select2("data",
[{"id":"test1","text":"Henry Ford"},{"id":"test2","text":"Tom Phillips"}]
);

谢谢

最佳答案

function formatSelection(val) {return val.id;}

$('select').select2({templateSelection: formatSelection,width:"300"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<select id="convertIt" multiple>
<option value="A">Australia</option>
<option value="U">US </option>
<option value="J">Japan</option>
<option value="E">Europe</option>
<option value="C">Canada</option>
</select>

关于javascript - 如何将值设置为在 select2 中显示而不是文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39504001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com