javascript - Bootstrap selectpicker "data-subtext" does not updating the live changes - Stack Overflow

I want to change my data-subtext attribute of the options of my select elementin runtime. But data-su

I want to change my data-subtext attribute of the options of my select element in runtime. But data-subtext does not get updated in selectpicker. Here's my code:

$('.selectpicker').attr("data-subtext","new subtext").selectpicker('refresh');

I want to change my data-subtext attribute of the options of my select element in runtime. But data-subtext does not get updated in selectpicker. Here's my code:

$('.selectpicker').attr("data-subtext","new subtext").selectpicker('refresh');
Share Improve this question edited May 29, 2015 at 13:53 jarlh 44.8k8 gold badges50 silver badges67 bronze badges asked May 29, 2015 at 5:34 prakashstar42prakashstar42 6871 gold badge6 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

That's because .selectpicker class stands for the select element itself not for it's options. Considering it can have more than one option you need to state which option's data-subtext you wish to change. refer to the following snippet:

$(document).ready(function() {
  $('#myBtn').click(function() {
    $(".selectpicker > option:eq(1)").data("subtext", "Look I'm changed");
    $(".selectpicker").selectpicker('refresh');
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.4/css/bootstrap.min.css" />
  <link rel="stylesheet" href="//cdnjs.cloudflare./ajax/libs/bootstrap-select/1.6.5/css/bootstrap-select.min.css" />
  <link rel="stylesheet" href="style.css" />
  <script src="http://code.jquery./jquery-2.1.4.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.4/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare./ajax/libs/bootstrap-select/1.6.5/js/bootstrap-select.min.js"></script>

 <select class="selectpicker">
        <option data-subtext="Mustard subtext">Mustard</option>
        <option data-subtext="Ketchup subtext">Ketchup</option>
        <option data-subtext="Relish subtext">Relish</option>
      </select>
      <a class="btn btn-default" id="myBtn">Update second options data-subtext</a>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745370951a4624790.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信