0

我正在通过 ajax 调用加载一些 html。

此 html 还包含<script>标签中的 javascript。

这些脚本标签运行,但我尝试将事件处理程序附加到<input slider>(JQM 库的一部分)的更改事件。

如果我使用 'deprecated' 这将有效.live()。问题是这个 html 被加载了几次,并且每次都重新添加每个事件处理程序。

如果我改为使用.on()DOCS 建议,则在更改输入值时不会触发事件。

"input type="range" name="betInput" id="Size-@sel.UniqueId" min="0" max="@sel.AvailableToBet"/"

// Using .live() here works but keeps adding
$('#Size-@sel.UniqueId').on('change', function () {

                    console.debug('Should print when slider-value is changed but is not');
                });


                // This next line will however trigger the event above.
                $('#Size-@sel.UniqueId').trigger('change');

使用 JQuery 1.8.2 和 JQM 1.2.0

4

2 回答 2

0

You need to add a selector to make the on act like live:

// Using .live() here works but keeps adding
$('#Size-@sel.UniqueId').on('change', function () {

Should be something like:

$('parentSelector').on('change', ,'#Size-@sel.UniqueId', function () {

Read more in the docs:

If selector is omitted or is null, the event handler is referred to as direct or directly-bound. The handler is called every time an event occurs on the selected elements, whether it occurs directly on the element or bubbles from a descendant (inner) element.

When a selector is provided, the event handler is referred to as delegated. The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. jQuery bubbles the event from the event target up to the element where the handler is attached (i.e., innermost to outermost element) and runs the handler for any elements along that path matching the selector.

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

于 2012-11-03T17:16:10.647 回答
0

尝试将您的活动从“更改”更改为“幻灯片停止”,如下所示:

$('#Size-@sel.UniqueId').on('slidestop', function () {

这个答案帮助了我:jQueryMobile 滑块更改事件

于 2018-10-11T19:30:10.103 回答