cflayout:getElementById 在包含在源文件中时不起作用

发布于 2024-11-29 08:14:02 字数 786 浏览 0 评论 0原文

如果我使用 cflayout 在 CF9 中创建一个布局,如下所示:

<cfajaximport tags="cfform">
<cflayout type="border" name="example">
    <cflayoutarea position="center" name="_center" source="/example/center.cfm" />
</cflayout>

然后创建一个要包含的 center.cfm 文件:

<cfform name="myForm" id="myForm" action="post">
    <cfinput name="field1" type="text"><br/>
</cfform>

<script>
    ThisForm = document.getElementById('myForm');
    alert(ThisForm.id);
</script>

我收到错误:

Error processing JavaScript in markup for element cf_layoutarea_center

但如果我将 center.cfm 的内容移动到标签中(并删除源参数)代码按预期工作。谁能告诉我如何使用 center.cfm 文件让它工作?或者解释一下为什么这不起作用?

感谢您抽出时间。

干杯 标记

If I create a layout in CF9 using cflayout as below:

<cfajaximport tags="cfform">
<cflayout type="border" name="example">
    <cflayoutarea position="center" name="_center" source="/example/center.cfm" />
</cflayout>

and then create a center.cfm file to be included:

<cfform name="myForm" id="myForm" action="post">
    <cfinput name="field1" type="text"><br/>
</cfform>

<script>
    ThisForm = document.getElementById('myForm');
    alert(ThisForm.id);
</script>

I get an error:

Error processing JavaScript in markup for element cf_layoutarea_center

But if I move the contents of center.cfm into the tag (and remove the source parameter) the code works as expect. Can anyone tell me how I can get this to work using the center.cfm file? Or explain why this doesn't work?

Thanks for your time.

Cheers
Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我喜欢麦丽素 2024-12-06 08:14:03

使用 ID 名称值调用函数参数。

我使用 ID 值调用 javascript 函数,然后将 ID 值与 getElementByID 一起使用:

<script>
myLoadFunction = function(clickid){
    ThisForm = document.getElementById(clickid);
    alert(ThisForm.id);
}
</script>

Call function argument with ID name value.

I call the javascript function with ID value, then use ID value with getElementByID:

<script>
myLoadFunction = function(clickid){
    ThisForm = document.getElementById(clickid);
    alert(ThisForm.id);
}
</script>

相思碎 2024-12-06 08:14:02

(丹已经提供了答案。但只是为了表明它可以工作..)

这对我来说在 CF9 下工作得很好。我不确定为什么添加标签对您不起作用。

test.cfm

<cfajaximport tags="cfform" />
<cflayout type="border" name="example">
    <cflayoutarea position="center" name="_center" source="myForm.cfm" />
</cflayout>

myForm.cfm

<html>
<head>
<script type="text/javascript">
    function myLoadFunction(){
        ThisForm = document.getElementById('myForm');
        alert(ThisForm.id);
    }
</script>
</head>
<body> 
<cfform name="myForm" id="myForm" action="post">
    <cfinput name="field1" type="text"><br/>
</cfform>
</body>
</html>
<cfset AjaxOnLoad("myLoadFunction") />

(Dan provided the answer already. But just to show it can work ..)

This worked fine for me under CF9. I am not sure why adding the tags did not work for you.

test.cfm

<cfajaximport tags="cfform" />
<cflayout type="border" name="example">
    <cflayoutarea position="center" name="_center" source="myForm.cfm" />
</cflayout>

myForm.cfm

<html>
<head>
<script type="text/javascript">
    function myLoadFunction(){
        ThisForm = document.getElementById('myForm');
        alert(ThisForm.id);
    }
</script>
</head>
<body> 
<cfform name="myForm" id="myForm" action="post">
    <cfinput name="field1" type="text"><br/>
</cfform>
</body>
</html>
<cfset AjaxOnLoad("myLoadFunction") />
桃酥萝莉 2024-12-06 08:14:02

您需要使用 AjaxOnLoad ColdFusion 方法,以便在子页面加载完成后触发 JavaScript。您需要在 center.cfm 中像这样定义您的函数:

<script>
myLoadFunction = function(){
    ThisForm = document.getElementById('myForm');
    alert(ThisForm.id);
}
</script>

然后,在 center.cfm 中的任何位置添加以下 ColdFusion:

<cfset AjaxOnLoad("myLoadFunction") />

一旦 center.cfm 完成加载,就会触发 myLoadFunction cflayout 标签。

编辑: Adobe LiveDocs 有一个关于如何处理动态包含的 javascript。

相关部分:

动态包含的页面上的所有 JavaScript 函数定义(例如通过使用绑定表达式、ColdFusion.navigate 函数或 ColdFusion Ajax 容器标记内的表单提交)必须具有以下语法格式:
函数名 = 函数(参数) {函数体}

使用以下格式的函数定义可能不起作用:
函数函数名(参数){函数体}

You need to use the AjaxOnLoad ColdFusion method in order to fire off your javascript once the child page finishes loading. You need to define your function like this inside of center.cfm:

<script>
myLoadFunction = function(){
    ThisForm = document.getElementById('myForm');
    alert(ThisForm.id);
}
</script>

Then, anywhere in center.cfm, add the following ColdFusion:

<cfset AjaxOnLoad("myLoadFunction") />

That should fire the myLoadFunction as soon as center.cfm has finished loading in your cflayout tag.

EDIT: The Adobe LiveDocs has a section on how to handle dynamically included javascript.

The relevant section:

All JavaScript function definitions on pages that you include dynamically, for example by using a bind expression, the ColdFusion.navigate function, or a form submission within a ColdFusion Ajax container tag, must have the following syntax format:
functionName = function(arguments) {function body}

Function definitions that use the following format might not work:
function functionName (arguments) {function body}

原野 2024-12-06 08:14:02

对我来说,center.cfm 文件需要有 html、body 和 body 标签似乎不太正确,所以我尝试了这个(Dan 和 Leigh 的解决方案的组合):

主页:

<html>
<head>
<script type="text/javascript">
    function myLoadFunction(){
        ThisForm = document.getElementById('myForm');
        alert(ThisForm.id);
    }
</script>
<cfajaximport tags="cfform">
</head>
<body> 
<cflayout type="border" name="example">
    <cflayoutarea position="center" name="_center" source="center.cfm" />
</cflayout>
</body>
</html>

然后 center.cfm 很简单:

<cfform name="myForm" id="myForm" action="post">
    <cfinput name="field1" type="text"><br/>
</cfform>
<cfset AjaxOnLoad("myLoadFunction") />

这按预期工作,并且我们不会在通过 AJAX 加载并添加到页面的内容中发送另一个 html 标签等。

It did not seem right to me that the center.cfm file needed to have the html, body and body tags, so I tried this (a combination of Dan & Leigh's solutions):

Main Page:

<html>
<head>
<script type="text/javascript">
    function myLoadFunction(){
        ThisForm = document.getElementById('myForm');
        alert(ThisForm.id);
    }
</script>
<cfajaximport tags="cfform">
</head>
<body> 
<cflayout type="border" name="example">
    <cflayoutarea position="center" name="_center" source="center.cfm" />
</cflayout>
</body>
</html>

And then center.cfm is simply:

<cfform name="myForm" id="myForm" action="post">
    <cfinput name="field1" type="text"><br/>
</cfform>
<cfset AjaxOnLoad("myLoadFunction") />

THis works as expected, and we are not sending another html tag, etc in content that is being loaded via AJAX and added to the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文