问题描述
限时送ChatGPT账号..我正在使用 select2 jquery 插件来重新格式化我的长选择选项.一切都按预期工作,除了当我提交表单时,id
值被保存为值,而不是值(在本例中为 lang
).我不知道我做错了什么.
I'm using the select2 jquery plugin to reformat my long select options. Everything works as expected, except when I submit the form the the id
value gets saved as the value, instead of the value (in this case lang
). I can't figure out what I'm doing wrong.
脚本如下所示:
var langs=[{id:0,lang:'English'},
{id:1,lang:'Afrikaans'},
{id:2,lang:'Albanian'},
{id:3,lang:'Zulu'}];
function format(item) { return item.lang; };
$("#field_4").select2({
placeholder: "Select your language",
data:{ results: langs, text: 'lang' },
formatSelection: format,
formatResult: format
});
这里是标记:
<input id="field_4" name="field_4" type="text" class="regular-text " value="3" />
当我在控制台中记录返回值时,我看到:
When I log the return value in the console I see this:
{Object}
O: Object
id: "1"
text: "Afrikaans"
更新:我在这方面取得了一些进展.如果我将以下内容添加到构造函数对象(对于 select2),我可以保存正确的值(而不是 id
).但是,当我添加到使用 AJAX 获取选项的 时,这不起作用.
UPDATE:
I made some progress on this. If I add the following to the constructor object (for select2) I can save the correct values (instead of the id
). However this doesn't work when I add to the <select>
s that use AJAX to get the options.
id : function(object) {
return object.lang;
}
推荐答案
我在开发者的帮助下解决了这个问题:https://github/ivaynberg/select2/issues/852#issuecomment-13478644
I figured this out with the assistance of the developer: https://github/ivaynberg/select2/issues/852#issuecomment-13478644
简而言之,有一个 id:
回调函数,用于将返回的 ID 映射到所需的输入值.
In short, there's an id:
callback function which handles mapping the returned ID to the desired input value.
这篇关于jQuery select2 重新格式化&lt;select>但提交选项 ID 而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论