2014-09-26 45 views
0

我有一個JavaScript函數,當單擊表單提交按鈕時會被調用來做一些驗證。如果一切正常,則執行普通的JavaScript確認窗口以確保用戶想要提交表單。但是,我想使用jConfirm來根據我的需要定製標題,而不是「所以,所以頁面說:」。jConfirm框沒有出現

這是我有:

function validate(form) { 
    var fieldcheck = true; 

    if(!checkUSPhone (form.elements["phonenumber"],true)) 
    fieldcheck = false; 
    if(!checkEmail (form.elements["emailaddress"],true)) 
    fieldcheck = false; 

    if(fieldcheck) { 
    jConfirm(("Submit Changes"), 'Are you sure?', function(fieldcheck) { 
     if(fieldcheck) 
     { 
      alert("True"); 
      fieldcheck = true; 
     } 
     else 
     { 
      alert("False"); 
      fieldcheck = false; 
     } 
    }); 
    } 

    return fieldcheck; 
} 

我使用jQuery V1.7.2,並有jquery.alert.js V1.0.3並鏈接到頁面的jquery.alert.css文件。

但是,當用戶提交表單時,沒有框出現,頁面提交。我已經看過這裏的其他例子,我的語法與所說的是正確的。

我錯過了什麼?

+0

不能從異步方法返回! – epascarello 2014-09-26 18:11:21

+0

我曾經爲了一些誤導性的原因添加了它。無論如何,jConfirm框都不會出現。 – braab 2014-09-26 18:22:27

+0

由於表單提交...你不能用這樣的對話框取消表單提交。將'return fieldcheck;'改爲'return false'並觀察「alert」。 – epascarello 2014-09-26 18:23:34

回答

0

像epascarello是在暗示,你只需要對你的代碼的一些變化:

function validate(form) { 
    var fieldcheck = true; 

    if(!checkUSPhone (form.elements["phonenumber"],true)) 
    fieldcheck = false; 
    if(!checkEmail (form.elements["emailaddress"],true)) 
    fieldcheck = false; 

    if(fieldcheck) { 
    jConfirm(("Submit Changes"), 'Are you sure?', function(fieldcheck) { 
     if(fieldcheck) 
     { 
      alert("True"); 
      form.submit();// The user has confirmed, we proceed to submit. 
     } 
     else 
     { 
      alert("False"); 
      fieldcheck = false; 
     } 
    }); 
    } 

    return false;// Prevent submit. We'll submit the form after the user has confirmed through jConfirm. 
}