1

按下删除按钮时,我希望弹出一个警报,以确保用户没有犯任何错误。

因此,在 codeIgniter 中,我的这个效果很好:

按钮:

<td><a class='Right btn btn-danger' onClick="ConfirmMessage('news', <?php echo $new->id ?>,'news')">
                        <i class="icon-remove-sign icon-white"></i>
                    </a></td>

Javascript:

function ConfirmMessage(type, id, types) {
    if (confirm("Are you sure you want to delete this "+type+" ?")) { // Clic sur OK
     document.location.href='<?php echo site_url(); ?>/'+types+'/delete/'+id;
    }
  }

但是现在,使用 Symfony2,我不能这样做:

按钮

<td><a class='Right btn btn-danger' onClick="ConfirmMessage('artist', {{ artist.id }},'news')">
                            <i class="icon-remove-sign icon-white"></i>
                        </a></td>

Javascript

function ConfirmMessage(type, id, types) {
    if (confirm("Are you sure you want to delete this "+type+" ?")) { // Clic OK
        document.location.href="{{ path('ymtest_Delete'"+types+", {'id': "+id+"}) }}";
    }
}

因为当 Symfony 想要生成一个 url 时我得到一个错误。

有什么解决办法?谢谢

4

1 回答 1

4

你不能写

 document.location.href="{{ path('ymtest_Delete'"+types+", {'id': "+id+"}) }}";

您正在混合树枝函数和 javascript 函数。Twig 将在后端解析和编译 收到 html 页面后,Javascript 将在您的客户端执行

您必须生成每个 url 类型并将其分配给 javascript 变量。

一种更优雅的方法是将您的 url 和消息存储在数据属性中

<a class="Right btn btn-danger" data-url="{{ path('ymtest_EditMusician', {'id': artist.id}) }}" data-message="Are you sure you want to delete this type ?" ></a>

您现在可以将事件单击与 javscript 绑定并检索您的数据属性值。没有更多的 javascript 测试

于 2013-02-06T22:17:08.093 回答