简体   繁体   English

Aria属性不会添加到结果中

[英]Aria attributes not be added to results

I am using select 2 version 4.0.2 and have created a select to look up remote data via ajax. 我正在使用select 2版本4.0.2,并创建了一个select以通过ajax查找远程数据。

I am getting the data ok but when the results are rendered, the aria-selected attribute is not being added and therefore I can not select any of the options. 我的数据正常,但是在呈现结果时,没有添加aria-selected属性,因此无法选择任何选项。

I believe it may be to do with the templateResult and templateSelection but I don't know what needs to be changed. 我相信可能与templateResult和templateSelection有关,但我不知道需要更改什么。

Jquery: jQuery:

$("#select2").select2({
  theme: "bootstrap",
  ajax: {
    url: "/search/games",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term, // search term
        type: "suggest"
      };
    },
    processResults: function (data, params) {
      return {
        results: data.games,
      };
    },
    cache: true
  },
  escapeMarkup: function (markup) { return markup; },
  minimumInputLength: 3,
  templateResult: formatResult,
  templateSelection: formatSelection
});

function formatResult(game) {
  markup = "<p>" + game.name + "</p>";
  return markup;
};

function formatSelection(game) {
  markup = "<p>" + game.name + "</p>";
  return markup;
}

Form: 形成:

= form_for [:member,@event], :html => {:class => "formtastic form-horizontal"} do |f|
  %fieldset
    %div{:class => "control-group"}
      = f.label :game_id, :class => "control-label"
      %div{:class => "controls"}
        = f.select :game_id, "", {}, :class => "form-control"

Turns out when processing the results, the data has to be mapped to an id and text attribute for it to be recognised: 事实证明,在处理结果时,必须将数据映射到id和text属性以进行识别:

processResults: function (data, params) {
  return {
    results: jQuery.map( data.games, function( game ) {
      var arr = {id:game.name,text:game.name};
      return arr;
    }),
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM