set dropdown selected value with backbone

2019-02-14 04:51发布

I want to set my dropdown menu from my js. Basically, i want to be able to set the updated drop down value from the js.

my.html

  <div class="dropDownList">
                    <select id="ddpFilter" >
                        <option value="1">Show 1</option>
                        <option value="2">Show 2</option>
                        <option value="3">Show 3</option>
                    </select>
                </div>

my.js

   events : {
     'change #ddpFilter' : 'Filter'
  },

  Filter : function(e){
      // Set Selected Value of the dropdown 
  }
});

标签: backbone.js
1条回答
Melony?
2楼-- · 2019-02-14 05:57

You should start your function names with a lowercase letter (as per Mr Crockford) and use more descriptive function names.

In order to set the value of a select list, use option[value='x'] syntax. Assuming you'd like to set the second option:

events : {
  'change #ddpFilter' : 'filterDdpSelect'
},

filterDdpSelect: function(e){
  var selectValue = 2;
  $("#ddpFilter option[value='" + selectValue + "']").attr("selected", "selected");
}
查看更多
登录 后发表回答