首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery转发器在使用ajax生成的部分中不起作用

jQuery转发器在使用ajax生成的部分中不起作用
EN

Stack Overflow用户
提问于 2017-05-14 01:35:38
回答 2查看 2.4K关注 0票数 1

在一个由ajax在单个页面中生成的多个步骤组成的表单中,我必须在中间步骤中使用jquery.repeater

特定于中继器的表单代码位于专用部分中

但是jquery.repeater无法工作,当我点击add或delete按钮时,没有任何反应,控制台中甚至没有一条javascript错误消息。

如果我在没有ajax加载的页面中直接使用这个部分,它就可以工作。

注意,我在我的ajax处理程序方法中使用了推送部分更新方法

return [ '#myDiv' => $this->renderPartial('mypartial') ];

欢迎帮助伙伴们。

下面是我使用的代码

以及我布局底部的脚本声明

代码语言:javascript
复制
...
<!-- Scripts -->
<script src="{{ [
    'assets/javascript/jquery-3.2.1.min.js',
]|theme }}"></script>
{% framework extra %}
{% scripts %}
</body>
</html>

我的部分

代码语言:javascript
复制
<form class="repeater">
    <!--
        The value given to the data-repeater-list attribute will be used as the
        base of rewritten name attributes.  In this example, the first
        data-repeater-item's name attribute would become group-a[0][text-input],
        and the second data-repeater-item would become group-a[1][text-input]
    -->
    <div data-repeater-list="group-a">

      <div data-repeater-item style="display:none;">
        <input type="text" name="text-input" />
        <input data-repeater-delete type="button" value="Delete" />
      </div>

      <div data-repeater-item>
        <input type="text" name="text-input" />
        <input data-repeater-delete type="button" value="Delete" />
      </div>

    </div>

    <input data-repeater-create type="button" value="Add" />
</form>

我的页面

代码语言:javascript
复制
title = "Ajouter"
url = "/tableau-de-bord/espace/ajouter"
layout = "dashboard"
is_hidden = 0

[formulaireCreation]
==
function onStart()
{
    $this->addJs('assets/javascript/node_modules/jquery.repeater/jquery.repeater.js');
    $this->addJs('assets/javascript/my-repeater.js');
}
==
<div id='formulaire-creation-component-wrapper'>
    {% component 'formulaireCreation' %}
</div>

my-repeater.js

代码语言:javascript
复制
$(document).ready(function () {
    $('.repeater').repeater({

        // (Optional)
        // "show" is called just after an item is added.  The item is hidden
        // at this point.  If a show callback is not given the item will
        // have $(this).show() called on it.
        show: function () {
            $(this).slideDown();
        },
        // (Optional)
        // "hide" is called when a user clicks on a data-repeater-delete
        // element.  The item is still visible.  "hide" is passed a function
        // as its first argument which will properly remove the item.
        // "hide" allows for a confirmation step, to send a delete request
        // to the server, etc.  If a hide callback is not given the item
        // will be deleted.
        hide: function (deleteElement) {
            if(confirm('Are you sure you want to delete this element?')) {
                $(this).slideUp(deleteElement);
            }
        },
    })
});

最后,为了进行比较,在基本页面中工作的代码

代码语言:javascript
复制
title = "repeater-test"
url = "/repeater-test"
layout = "dashboard"
is_hidden = 0
==
function onStart()
{
    $this->addJs('assets/javascript/node_modules/jquery.repeater/jquery.repeater.min.js');
    $this->addJs('assets/javascript/my-repeater.js');
}
==
<form class="repeater">

    <div data-repeater-list="group-a">

      <div data-repeater-item style="display:none;">
        <input type="text" name="text-input" />
        <input data-repeater-delete type="button" value="Delete" />
      </div>

      <div data-repeater-item>
        <input type="text" name="text-input" />
        <input data-repeater-delete type="button" value="Delete" />
      </div>

    </div>

    <input data-repeater-create type="button" value="Add" />

</form>
EN

回答 2

Stack Overflow用户

发布于 2017-05-15 20:21:19

好吧,我找到了一个解决方案,虽然不是很优雅,但很管用

当带有中继器的分部在ajax中加载时,就好像页面在页面级别忘记了用addJS声明的脚本一样,所以我在分部的末尾直接使用<script src="/path/to/myscript.js">声明了脚本,它可以正常工作

尝试使用{% put scripts %}注入脚本,但不起作用

票数 0
EN

Stack Overflow用户

发布于 2018-08-31 01:59:52

使用JqueryRepeater就像在地狱中工作一样。从视觉上看,这是非常棒的,但编写基础之外的代码就像每次都踢了gg一脚!

jQuery似乎不能很好地检测每一行的新字段,但使用纯JavaScript是可能的。

例如,要更改新生成的输入的值...

代码语言:javascript
复制
Document. getElementsByName ( "your-data-repeater-list[1][field_name]")[0].Value =  "My new value";

其中1是新的中继器编号

我花了好几天(和几个晚上!)使用这个组件,再加上一些耐心,你可以做任何你想做的事情。

玩得开心!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43956080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档