2

我的代码中有类似的东西:

<?php foreach($clients as $client): ?>
    <tr class="tableContent">
        <td onclick="location.href='<?php echo site_url('clients/edit/'.$client->id ) ?>'"><?php echo $client->id ?></td>
        <td>
            <a class='Right btn btn-danger' onClick="ConfirmMessage('client', <?php $client->id ?>,'clients')">
                <i class="icon-remove-sign icon-white"></i>
            </a>
        </td>
    </tr>
<?php endforeach  ?>

这实际上是视图。因此,当用户单击删除按钮(带有 btn-danger 类的按钮)时,我希望他使用 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;
    }
}

所以这是我的问题:

我希望将 $type 替换为我将传递给函数的参数(如 client、article、post .. )。我也想获取$client->id参数。我不擅长 javascript,正如你已经猜到的那样,它显然根本不起作用。

4

2 回答 2

8

将 JavaScript 中的字符串与+(相对于.PHP)连接起来:

confirm("Are you sure you want to delete this " + type + " ?");
于 2012-10-02T21:37:09.280 回答
3

confirm只接受一个参数(一个字符串),即显示给用户的消息。
如果要在该消息中插入变量,则应使用+运算符连接字符串:

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;
    }
}
于 2012-10-02T21:37:54.463 回答