jQuery select2 重新格式化<select>但提交选项 ID 而不是值 [英] jQuery select2 reformats <select> but submits the option ID instead of the value

查看:17
本文介绍了jQuery select2 重新格式化<select>但提交选项 ID 而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 获取选项的

发送“验证码”获取 | 15天全站免登陆