【问题标题】:select2 default value for single fieldselect2 单个字段的默认值
【发布时间】:2014-02-18 09:33:48
【问题描述】:

我在我的项目中使用Jquery Select2。加载页面时,我想在单个输入字段中保留默认值。我通过设置 initSelection 来尝试这个,同时我正在像这样启动 select2 组件。

   $(document).ready(function() { 
        allowClear: true,
        ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
            dataType: 'json',
            url: "public/data.php",
            data: function (term, page) {
                return {
                    q: term, // search term
                    component: "map",
                    all_selected: $("#map-all").hasClass("active"),
                    parent_value: $("#city").val(),
                    child_value: ""
                };
            },
            results: function (data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data};
            }
        },
        initSelection: function(element, callback) {
            return $.getJSON("public/data.php?q="+element.val()+"&component=map&all_selected=false&parent_value=&child_value=", null, function(data) {
              if ($.isFunction(callback)) {
                return callback(data);
              }
            });
        },
        formatSelection: format,
        formatResult: format,
});

但是,这并不能正常工作。

但是,当我将multiple: true, 作为一个 select2 选项时,这非常有效。如何对单个隐藏字段执行此操作?

感谢和问候!

【问题讨论】:

    标签: jquery-select2


    【解决方案1】:

    好的,我通过将回调从 return callback(data); 更改为 return callback(data[0]); 解决了这个问题。我认为这背后的原因是因为该字段不是多个字段,它只接受一个对象到回调函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 2020-01-24
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2015-02-28
      相关资源
      最近更新 更多