JQuery .on()更改事件未触发(页面在.load()内)(JQuery .on() change event not firing(page is inside a .load()))

编程入门 行业动态 更新时间:2024-10-26 07:41:09
JQuery .on()更改事件未触发(页面在.load()内)(JQuery .on() change event not firing(page is inside a .load()))

好吧,所以我一直在寻找这个问题的答案,现在几个小时都发现没有任何效果。

我有一个页面,我在.click事件后加载到div中,所以看起来像这样:

$('#mybutton').click( function () { $('#mydiv').load('myinnerpage.aspx'); });

然后我在myinnerpage.js里面我有:

$(document).on('change', 'input[name=rbfileByNameOrID]:radio', function () { if ($('input#rbByFileName').attr('checked')) fileSelectionBy('filename'); else fileSelectionBy('fileid'); });

更改函数永远不会被触发,除非我自己打开myinnerpage.aspx。

Okay, so I've been looking for an answer for this for a couple of hours now and found nothing that works.

I have a page that I load inside a div after a .click event, so that looks like this:

$('#mybutton').click( function () { $('#mydiv').load('myinnerpage.aspx'); });

Then I have inside myinnerpage.js I have:

$(document).on('change', 'input[name=rbfileByNameOrID]:radio', function () { if ($('input#rbByFileName').attr('checked')) fileSelectionBy('filename'); else fileSelectionBy('fileid'); });

the change function never gets fired, unless I open myinnerpage.aspx by itself.

最满意答案

您可以将myinnerpage.js代码放在主javascript中,而不是放在包含的页面中吗?

如果你不能,我不是“好解决方案”,但这可以帮到你:jQueryMobile-Navigation在用ajax加载页面时遇到了同样的问题,并且他们使用了非jquery解决方案来解决这个问题。 在他们的$ .ajax->成功中,他们使用innerHTML:

[...] //workaround to allow scripts to execute when included in page divs all.get( 0 ).innerHTML = html; [...]

你可以在这里看到他们的完整代码。

它适用于页面div中包含的script-tag 和inline-javascript

Can you put your myinnerpage.js code in your main javascript, instead of in your included page ?

If you can't, i've not the "good-solution", but this can help you : jQueryMobile-Navigation had the same problem when they loaded page with ajax, and they used a non-jquery solution to solve that. In their $.ajax->success, they use innerHTML :

[...] //workaround to allow scripts to execute when included in page divs all.get( 0 ).innerHTML = html; [...]

You can see their full code here.

It works with script-tag AND inline-javascript included in page divs.

更多推荐

本文发布于:2023-07-05 08:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1035456.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   页面   load   JQuery   firing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!