从Select2 ajax调用获取表单中的选定数据

Pur*_*rus 4 jquery jquery-select2

我在HTML表单中使用Select2 jQuery插件通过ajax调用获取远程数据.此功能正常.

但是,在提交表单时,Select2元素中的选定值在POST数据中不可用.

我能够获取有关单选按钮和提交按钮的数据,但不能获取与Select2绑定的userbox元素.

如何在表单中获取所选数据以便在PHP中进行处理?

<form name='adminForm' method="post">
       <input type="radio" name="type" value="all"> 1 <br/>
       <input type="radio" name="type" value="from"> 2 <br/>
       <input type="radio" name="type" value="to"> 3 <br/>

       <div id="userbox"></div>

       <input type="submit" value="Submit" name="searchMessages">

</form>
Run Code Online (Sandbox Code Playgroud)

下面是我使用的jQuery代码.

$("#userbox").select2({
    minimumInputLength: 1,
    formatResult: productFormatResult, // Not provided here
    formatSelection: productFormatSelection, // Not provided here
    ajax: { 
        url: "/url/path",
        data: function (term, page) {
            return {
                q: term,
            };
        },
        results: function (data, page) {
            return {results: data};
        }
    },
    escapeMarkup: function (m) { return m; }
});
Run Code Online (Sandbox Code Playgroud)

小智 6

重点是,

无论何时提交表单,它都会发送所有输入标记,只发送具有名称的标记

属性.它就像一个键值映射,输入名称为键,值为值.

我发现当我选择任何项目时,没有输入控件来保存所选值,

所以你可以准备一个隐藏的字段来保存所选的值,你会的

从后端代码中获取值