2010-04-07 65 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); 
}