0

This is a strange one..

Essentially I have a three stage form which automatically saves it status.

When you click the first submit button it loads the second stage through ajax.

If you refresh the page it reloads the second stage (but NOT through ajax) using session data.

On the second stage of the form is a button.. when the user clicks it I want to call some jquery and make an alert.

My code is as follows:

    $(document).on("click","#submity",function(event){
event.preventDefault();
alert('lol');


    });

This is included between a document ready statement in an external js file loaded on the initial page load. It is loaded after the jQuery library, and that is being imported correctly.

When the second page is loaded by ajax, clicking the button does absolutely nothing. When loaded directly it alerts as expected. I cannot for the life of me work out why?

Other questions on SO seem to pertain to delegating with on() but as far as I am aware I have done it correctly, and similar setups work elsewhere on the site..

Any ideas?

Thanks

4

2 回答 2

0

你可能有一个对象document#submity一个阻止事件冒泡的点击处理程序,或者使用 event.stopPropagation() 或返回 false;

你的代码是这样工作的:click 事件在被点击的对象 ( #submity) 上创建并向上传递 DOM 层次结构,直到它到达documentjQuery 调用事件处理程序的位置。如果该链在中间某处被取消,您的处理程序将永远不会被调用。

您是否可能在 ajax 调用期间或之后分配单击事件处理程序?

没有代码将很难诊断。你能发个链接吗?

于 2013-02-26T12:25:07.303 回答
-1

您必须在页面上加载动态加载按钮后绑定 .on() 事件,或者使用已过时的 .live() 事件。所以在ajax成功回调中绑定东西

于 2013-02-26T12:09:55.720 回答