AJAX 更新 div 而不创建部分

AJAX update div without creating partial

我有一些页面:

title = "page"
url = "/page"
layout = "default"
==
function onTest()
{
    $this['result'] = 'Test string';
}
==

<!-- AJAX enabled form -->
<form data-request="onTest" data-request-update="mypartial: '#myDiv'"></form>

<!-- Result container -->
<div id="myDiv"></div>

提交表单后,我只能在部分 'mypartial' 中获得 'result' 变量,其中代码为:Here is {{ result }}。这段代码插入#myDiv.

如何在不创建局部变量的情况下获取 'result' 变量?为什么我应该为每个小 AJAX 查询创建 .htm 文件(部分)?

我猜你可以很容易地从服务器端用 id myDiv 更新你的 div。

You just need to push markup with proper id #myDiv it will update that Html Element with given ID automatically. [ we do not need to ask for partial or need partial ]

title = "page"
url = "/page"
layout = "default"
==
<?php
function onTest()
{
    $result = 'Test string';    
    return ['#myDiv' => 'New Result: ' . $result]; 
}
?>
==
<!-- AJAX enabled form -->
<form data-request="onTest">
    <button type="submit">Submit Fire Ajax</button>
</form>

<!-- Result container -->
<div id="myDiv">old result</div>

After successful Ajax request markup for myDiv will be

<div id="myDiv">New Result: Test string</div>

Good thing is you do not need to write javascript to update myDiv as part of October Ajax framework it will automatically search #myDiv and update its markup.

当您 return 十月 CMS Ajax api 的数组时

return ['#myDiv' => 'Hello'];

在客户端,它会自动搜索 ID 为 myDiv 的元素,如果找到它,它会在 Ajax 请求成功时用给定的标记更新它,在这个例子中它是 Hello

如有疑问请评论。