且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails-Ajax选择菜单仅触发一次?

更新时间:2023-08-17 18:01:34

因此,您的更新正在创建一个新元素,您的更改事件不适用于该新元素. JS更改绑定到执行时存在的元素.解决此问题的一种简单方法是添加:

So your update is creating a new element to which your change event does not apply. The JS change binds to elements that exist at the time of execution. One easy way to fix the issue is to add:

$('.status-change-selector').unbind('change');
$('.status-change-selector').change(function(event) {
  event.preventDefault();
  $(this).closest('form').submit();
});

到Update.js.erb的末尾.这将绑定新创建的DOM元素.其他更干的解决方案要复杂一些,但基本上涉及将侦听器附加到您要插入的DOM的父元素上,而不是附加到元素本身上.

to the end of your Update.js.erb. That will bind the newly created DOM element. Other solutions which are more DRY are a little more complex, but basically involve attaching a listener to a parent element of the DOM you're inserting rather than to the element itself.