如何使用Twig AJAX表格同时调用2个函数? [英] How to call 2 Functions at the same time using Twig AJAX Form?

查看:83
本文介绍了如何使用Twig AJAX表格同时调用2个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于 OctoberCMS ="nofollow noreferrer"> Laravel 和 Twig .

I'm using OctoberCMS based on Laravel and Twig.

我正在使用具有两个按钮的 Twig AJAX表单.每个调用PHP函数actionOne()actionTwo().

I'm using a Twig AJAX Form that has 2 buttons. Each call a PHP function, actionOne() and actionTwo().

如何使第二个按钮同时调用两个功能?

How do I make the second button call both functions at the same time?

在一个按钮上使用多个data-request不起作用.并且在data-request中使用逗号分隔的多个功能也不起作用.

Using multiple data-request on a button doesn't work. And using multiple functions separated by comma in data-request also doesn't work.

表格

{{ form_open() }}

    <button type="button" data-request="actionOne">Action 1</button>
    <button type="button" data-request="actionTwo">Action 1 & 2</button>

    <input type="checkbox" name="queuedOne[]" value="{{ record.one }}" />
    <input type="checkbox" name="queuedTwo[]" value="{{ record.two }}" />

{{ form_close() }}  

我已经更正了数据请求和名称的值.

I have corrected the values of data-request and name.

推荐答案

有两种不同的方法可以实现此目的.

There are a couple of different ways you could accomplish this.

第一个选项,您可以创建第三个PHP方法,该方法只完成您要完成的所有事情,然后调用该方法:

First option, you could create a third PHP method that simply does both of the things that you want to accomplish and then call that instead:

function onActionThree() {
    onActionOne();
    onActionTwo();
}

<button data-request="actionThree">Click me for both previous actions</button>

第二个选项,您可以利用 AJAX Framework JS API 来创建两个单独的AJAX请求actionOneactionTwo.

Second option, you could utilize the AJAX Framework JS API to make two separate AJAX requests to actionOne and actionTwo.

<button id="action-three-btn">Click me for both previous actions</button>

<script>
   jQuery(document).ready(function ($) {
       $('#action-three-btn').on('click', function (e) {
           e.preventDefault();
           $(this).request('actionOne');
           $(this).request('actionTwo');
       });
   });
</script>

我将保留最后的评论,请阅读 https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem http://xyproblem .info/.对问题的这种模糊的抽象并不能帮助任何人真正解决您的实际问题.

I'll leave this off with a final comment, please read https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem and http://xyproblem.info/. This vague abstraction of your questions isn't helping anyone actually address your real problem.

这篇关于如何使用Twig AJAX表格同时调用2个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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