【问题标题】:select2, how to change before showselect2,如何在show前更改
【发布时间】:2017-03-22 12:30:24
【问题描述】:

我正在使用 select2 来构建我的组合。

一些元素被禁用,但我想用 aria-required="false" 粗体显示它们

var data= [{
     id="1",
     value = "first",
     disabled:true
},{
     id="2",
     value = "second",
     disabled:false
},

];
function formatResult(node) {
  if(node.disabled){
    var $result = $('<span><strong>' + node.text + '</strong></span>');
  } else {
    var $result = $('<span>' + node.text + '</span>');
  }
  return $result;
}

$("#myCombo").select2({
  placeholder: 'Seleccione una opción',
  width: "350px",
  data: data,
  formatSelection: function(item) {
    return item.text
  },
  formatResult: function(item) {
    return item.text
  },
  templateResult: formatResult
});

};

这行得通,但是 li :

<li class="select2-results__option" role="treeitem" aria-disabled="true">
        <span style="padding-left:20px;"><strong>first</strong></span>
</li>

当它打开时,使用调试器,如果我执行:

$('.select2-results__option').attr('aria-disabled',false);

它可以按我的意愿工作,但我无法以编程方式执行此操作,似乎不存在 beforeShow 函数,我该怎么做?

【问题讨论】:

    标签: jquery select2 jquery-select2-4


    【解决方案1】:

    通常,您可以通过超时来实现此目的。

    $("#myCombo").select2({
      placeholder: 'Seleccione una opción',
      width: "350px",
      data: data,
      formatSelection: function(item) {
        return item.text
      },
      formatResult: function(item) {
        return item.text
      },
      templateResult: formatResult
    });
    setTimeout(function(){
      $('.select2-results__option').attr('aria-disabled',false);
    });
    

    【讨论】:

    • 它可以工作,但我应该将 setTimeout 放在我的 formatResult 函数中,因为每次单击选择时都会调用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2016-02-03
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多