【问题标题】:How to pass a parameter in a Javascript confirm function?如何在 Javascript 确认函数中传递参数?
【发布时间】:2012-10-02 21:34:38
【问题描述】:

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

<?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-&gt;id 参数。 我的 javascript 很差,正如你已经猜到的那样,它显然根本不起作用。

【问题讨论】:

    标签: php javascript confirm


    【解决方案1】:

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

    confirm("Are you sure you want to delete this " + type + " ?");
    

    【讨论】:

      【解决方案2】:

      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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多