AJAX更新div而不创建部分 [英] AJAX update div without creating partial

查看:83
本文介绍了AJAX更新div而不创建部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些页面:

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.

After submitting the form I can get 'result' variable only in partial 'mypartial', where code is: Here is {{ result }}. And this code inserts into #myDiv.

如何在不创建局部变量的情况下获取结果"变量?为什么要为每个小的AJAX查询创建.htm文件(部分)?

How can I get 'result' variable without creating partial? Why should I create .htm file (partial) for each little AJAX query?

推荐答案

我想您可以轻松地从服务器端更新ID为myDiv的div.

I guess you can easily update your div with id myDiv from server side.

您只需要推送具有正确ID #myDiv的标记,它将自动更新该Html Element with given ID. [我们不需要部分补偿,也不需要部分]

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>

successful AjaxmyDiv的请求标记之后将是

After successful Ajax request markup for myDiv will be

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

好事是,您不需要编写JavaScript来更新myDiv,因为它是October Ajax框架的一部分,它将自动搜索#myDiv并更新其标记.

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.

当您返回十月Cms的Ajax api的数组

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

在客户端,它将自动搜索ID为myDiv的元素,如果找到了它,则它将在成功的Ajax请求中使用给定的标记对其进行更新,在本例中为Hello

At client side it will automatically search element with id myDiv and if it found it, It will update it with given markup on successful Ajax request, for this example its Hello

如有疑问,请发表评论.

If any doubt please comment.

这篇关于AJAX更新div而不创建部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆