为CakePHP创建自定义确认弹出窗口

时间:2013-09-04 18:38:17

标签: php javascript cakephp

我正在处理编辑预订并根据此调整付款。如果预订延长,那么我需要向客户收取相应的费用,同样,如果缩短了我需要退款。

我试过这样的事情(没有实际发布任何东西,只是一个确认框),但即使按下取消,它仍然提交了表格。

<?php echo $this->Form->create('edit',array('onsubmit'=>'return check_dates()')); ?>
//rest of form here

<script>
function check_dates(){
   //make POST request to check previous reservation duration
   if(data.duration == 'longer'){
       confirm('The guest will be charged $X. Click OK to continue');
   }
   if(data.duration == 'shorter'){
       confirm('The guest will be refunded $X. Click OK to continue');
   }
   if(data.duration == 'no_change'){
       confirm('Click OK to continue')
   }
}
</script>

有可能做这样的事吗?正如我之前提到的,当我按下时取消提交的表格。

1 个答案:

答案 0 :(得分:1)

查看此实时演示:http://jsbin.com/iZididi/2

在check_dates()中,您必须设置return。如果您希望表单提交任何答案,请确认您已将最后一行替换为

function check_dates(){
     data = new Object();
  data.duration = 'shorter';

  if(data.duration == 'longer'){
       a = confirm('The guest will be charged $X. Click OK to continue');
   }
   if(data.duration == 'shorter'){
       a = confirm('The guest will be refunded $X. Click OK to continue');
   }
   if(data.duration == 'no_change'){
      a = confirm('Click OK to continue')
   }

  //return a;
  return true;
}