2010-04-07 89 views
2
<script LANGUAGE="JavaScript"> 
function confirmSubmit() { 
    jConfirm('Is the Appointment Confirmed?', 'Confirmation Dialog', function(r) { 
     if(r) { 
      return true; 
     } else { 
      return false; 
     } 
    }); 
} 
</script> 

<form name='cancel_form'id='cancel_form' method='POST' action=""> 
<center> 
<input type='submit' name='confirm_appointment' value='Cancel Appointment' onclick='return confirmSubmit();'> 
</center> 
</form> 

<script type='text/javascript'> 
var ajax_load = "<img class='loading' src='img/load.gif' alt='loading...' />"; 
var saveUrl = "<?php echo $this->url(array('controller' => 'appointment', 'action' =>'cancelsave'));?>"; 
$('#cancel_form').ajaxForm({ success: saveCallbk , url : saveUrl }); 
function saveCallbk(responseText) { 
    jAlert(responseText,'Alert Dialog'); 
    if(responseText.indexOf("ERROR")<0) { 
     $(location).attr('href',redirectUrl); 
    } 
} 
</script> 

当我提交表单时,我调用这个函数并使用jQuery的jConfirm。我打印r。它的打印正确(例如,truefalse),但return falsereturn true没有任何影响 - 它只显示弹出窗口并提交表单,不等待确认。如何解决这个问题?jquery返回false形式

ajaxForm插件负责本身的提交,它需要一个提交按钮。如果我使用:

function confirmSubmit() { 
    var agree=confirm("Is the Appointment Cancelled?"); 
    if (agree) { 
     return true; 
    } else { 
     return false; 
    } 
} 

喜欢默认的JavaScript效果很好

+0

你可以在你使用这个函数的地方包含代码吗?另外,请查看http://stackoverflow.com/editing-help,了解如何在问题中设置代码格式。 – 2010-04-07 18:22:00

回答

3

使用Type =“按钮”,而不是类型=“提交”,并附上此表单按钮的单击事件。

$('#button').click(function() { 
    jConfirm('Is the Appointment Confirmed?', 'Confirmation Dialog', function(r) { 
     if (r) { 
      $('#form').submit(); 
     } 
    }); 

}); 
+0

谢谢你这个简单的解决方案..... – Fergus 2013-10-03 02:17:57

1

伊沃说什么。

您的函数confirmSubmit应返回true或false。

编辑 -

我不熟悉jConfirm,但你可能需要从jConfirm返回的结果,这样的。

<script> 
function confirmSubmit() 
{ 
    return jConfirm('Is the Appointment Confirmed?', 'Confirmation Dialog',   
     function(r) {  
      if(r){return true;} else {return false;} 
     }); 
} 
</script> 

如果是这样的话,你可以用的confirmSubmit(废除)干脆只说:

$('#form').submit(function() { 
    return jConfirm('Is the Appointment Confirmed?', 'Confirmation Dialog', function(r) { return r; }); 
}); 

希望这有助于...

荡即伊沃好: - )就我个人而言,我做了Ivo展示的东西。创建一个输入type="button",然后委派一个点击功能。

+0

$('#form')。submit(function(){ return jConfirm('预约确认?','确认对话',函数(r){返回r ;}); }); 我曾尝试过..也没有工作。 – Hacker 2010-04-08 04:44:00

1

这是我如何取消使用JavaScript和jQuery提交事件:

首先我有一个效用函数调用cancelEvent:(这是我从this blog entry拾起)

function cancelEvent(e) 
{ 
    e = e ? e : window.event; 
    if(e.stopPropagation) 
      e.stopPropagation(); 
    if(e.preventDefault) 
      e.preventDefault(); 
    e.cancelBubble = true; 
    e.cancel = true; 
    e.returnValue = false; 
    return false; 
} 

然后,我将有主要的JavaScript文件将包含这样的代码:

function validateForm(e) 
{ 
    var validated = true; 
    /* 
    Validation code goes here. 
    If validation fails set validated to false 
    */ 
    //If validation fails then at the end I'll want to cancel the submit event 
    if(validated) 
    { 
     return true; 
    } 
    return cancelEvent(e); 
} 

jQuery(document).ready(function() 
{ 
    jQuery("#theForm").submit(validateForm); 
}