2012-03-03 24 views
0

在我first javascript我顯示alerts如果上課checkemptysubmitting之前,如果一切都充滿然後在second javascript我顯示的是confirm submit?警告任何text box。但如何使這兩個作爲一個JavaScript代碼?集成了兩個JavaScript的代碼轉換成一個代碼來顯示警報

<script type="text/javascript">  
    jQuery('input.test').not('[value]').each(function() { 
     var blankInput = jQuery(this); 
     //do what you want with your input 
    }); 

    function confirmation(domForm) { 
     var jForm = jQuery(domForm); 
     var jFields = jForm.find('.check');; 
     var values = jFields.serializeArray(); 
     var failedFields = []; 

     for(var i = 0; i < values.length; i++) { 
      var o = values[i]; 
      if(o.value == null || o.value.length == 0) { 
       failedFields.push(jFields.filter('[name=' + o.name + ']').attr('title')); 
      } 
     } 

     if(failedFields.length > 0) { 
      var message = ''; 

      if(failedFields.length == values.length) { 
       message = 'fill all fields please'; 
      } 
      else { 
       message = 'please fill the fields:'; 
       for(var i = 0; i < failedFields.length; i++) { 
        message += "\n"; 
        message += failedFields[i]; 
       } 
      } 

     csscody.alert(message); 

     return false; 
    } 

    var answer = confirm("Confirm save?") 

if (answer){ 
     window.location = "confirmsubmit.jsp"; 
} 
else{  
     return false; 
} 

    return true; 
} 
</script> 

JavaScript來顯示其之後類check文本框confirm submit警報充滿

<script type="text/javascript"> 
$().ready(function() { 
    $('#btn_submit').click(function(e) { 
     e.preventDefault(); 

     var that = this; 
     var text = "Confirm save?"; 

     csscody.confirm(text, { 
      onComplete: function(e) { 
       if (e) { 
        window.location = "confirmsubmit.jsp"; 
       } 
       else { 
        return false; 
       } 
      } 
     }) 
    }); 
}); 
</script> 

HTML

<form action="confirmsubmit.jsp" onsubmit="return confirmation(this)" method="POST"> 
    <input type="text" class="check"/>//alert if text box is left empty 
    <input type="submit" id="btn_submit"/> 
</form> 
+0

任何人都可以請給我一個解決方案嗎? – Tom 2012-03-03 11:13:42

回答

0

我不明白爲什麼你需要第二個腳本。您可以調用驗證器功能onsubmit。爲什麼要改變window.location當您設置了相同的action?沒有必要將相同的功能綁定到按鈕的點擊事件上。

您不需要第二個腳本,但必須更改第一個腳本。

function confirmation(domForm) { 

    // Your other code 
    // ... 

    if(failedFields.length > 0) { 

     // Your other code 
     // ... 

     csscody.alert(message); 
     return false; 
    } 

    // Your other code 
    // ... 

/* Solution before your comment: 

    var answer = confirm("Confirm save?") 
    // This is already the action-target: window.location = "confirmsubmit.jsp"; 
    return answer; 
*/ 

    var text = "Confirm save?"; 

    csscody.confirm(text, { 
     onComplete: function(e) { 
      if (e) { 
       // Probably doesn't work because this seems to be asynchronous? 
       return true; 
      } 
      else { 
       return false; 
      } 
     } 
    }); 

} 
+0

感謝Smamatti你的答案,但由於'csscody.confirm();'我必須包括第二個腳本。你可以用'csscody.confirm()'編輯你的最後3行嗎?所以它會顯示一個警告框,即'確認保存?' – Tom 2012-03-03 12:13:47

+0

我不知道我是否理解你想要什麼,但我編輯了答案。我想這可能無法工作,因爲它可能** **被異步無需等待確認對話框的結果執行。 – Smamatti 2012-03-03 12:29:50

+0

是的,我將你的代碼,但警報不來,我點擊'提交button'後直接去'confirmsubmit.jsp'頁面,在這裏我錯了?請幫幫我 – Tom 2012-03-03 12:34:14