![](/img/trans.png)
[英]Jquery change event not fired when using selected attribute on select item
[英]Backbone change event not fired for select field selected by jQuery
我有一個按鈕,當單擊時設置選擇字段的值,並且還有一個選擇字段的更改事件。 'change select [name = location_id]'事件在通過鼠標/鍵盤正常選擇時有效,但點擊使用jQuery更改選擇字段值的按鈕,不會觸發'change select [name = location_id]'事件。
如何在通過jQuery更改字段時觸發“更改選擇[name = location_id]”?
SubscriptionsModule.Views.SubscriptionEditView = Backbone.Marionette.LayoutView.extend({
template: "subscriptions/edit",
events: {
'click #home-delivery-select': 'selectHomeDeliverySite',
'change select[name=location_id]': 'updateLocation'
},
selectHomeDeliverySite: function() {
this.$el.find('select[name=location_id]').val(this.model.get('hd_location_id'));
},
updateLocation: function(e) {
//update location code
},
updateOrderSize: function(e) {
SubscriptionsModule.eventManager.trigger('updateOrderSize', $(e.currentTarget).data('price'));
}
});
另外,請忽略下面的答案,因為按照該MDN input
事件的文檔中, input
活動只火的<input>
和<textarea>
並不會觸發<select>
,因為value
的<select>
不改變當選擇任何選項時(順便說一句,帶有復選框或收音機 type
<input>
也不會發出input
事件,因為它們的value
屬性在選擇時沒有改變( ! )。
我會留下答案供參考。
來自MDN
當用戶提交對元素值的更改時,將對
<input>
,<select>
和<textarea>
元素觸發change事件。 與輸入事件不同,對於元素值的每次更改,都不一定會觸發change事件。
根據Quirksmode的說法, change
事件對所有IE和Opera來說都是錯誤的。
嘗試更換
'change select[name=location_id]': 'updateLocation'
同
'input select[name=location_id]': 'updateLocation'
您只需要添加一個事件:
this.$('select[name=location_id]').val(this.model.get('hd_location_id')).trigger('change');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.