好吧,所以我一直在寻找这个问题的答案,现在几个小时都发现没有任何效果。
我有一个页面,我在.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.
更多推荐
发布评论