JavaScript确认删除不起作用

时间:2015-03-11 11:06:01

标签: javascript php cakephp

我正在使用cakephp 2.x版。

删除任何记录时遇到一个问题。我想在删除我的记录之前显示确认消息。 JavaScript onclick功能不起作用。请查看以下代码。

代码:

<?php
  echo $this->Html->image('delete.png', array(
  'title' => 'Delete',
  'alt' =>'Delete' ,
  'url' => array(
    'controller' => 'products',
    'action' => 'deletequery/'.$query['Query']['id']),array('confirm' => 'Are you sure you want to delete?')
    )
  );
?>

输出:

<a href="/products/deletequery/15">
   <img delete?="Are you sure you want to delete?" to="" want="" sure="" you="" are="" alt="Delete" title="Delete" src="/img/delete.png">
</a>

2 个答案:

答案 0 :(得分:1)

更改您的代码,因为您的代码不正确。

<?php 
 echo $this->Html->link($this->Html->image('delete.png'),
    array(
        'controller'=>'products',
        'action'=>'deletequery',$query['Query']['id']
    ),
    array('confirm'=>'Are you sure you want to delete?','escape'=>false)
 );
?>

<强>输出

<a 
  onclick="if (confirm('Are you sure you want to delete?')) 
  { return true; } return false;" 
  href="/ABC/products/deletequery">
  <img alt="" src="/ABC/img/delete.png">
</a>

答案 1 :(得分:1)

  

你应该在你的视图文件中尝试这个,基本上有两种删除文件的方法。   首先,你可以简单地制作cakephp $this->Html->link或seceond方法,你可以使$this->Form->postLink两者都不同。两者之间的差异是postLink创建一个&#34;表格标签&#34;但是link标签没有创建&#34;表格&#34;标签

     

<强> 1。 $这 - &GT; HTML-&GT;链路

<?php 
echo $this->Html->link($this->Html->image('delete.png').'', array('controller' => 'products', 'action' => 'deletequery',$query['Query']['id']),array('confirm'=>'Are you sure to delete ?','escape'=>false));
?> 

<强> 2。 $这 - &GT;形状配合&GT; postLink

<?php 
 echo $this->Form->postLink($this->Html->image('delete.png').'', array('controller' => 'products', 'action' => 'deletequery',$query['Query']['id']),array('confirm'=>'Are you sure to delete ?','escape'=>false));
 ?> 

你可以访问cakephp博客教程 cakephp blog tutorial