将两个javascripts代码集成到一个代码中以显示警报

时间:2012-03-03 10:14:34

标签: javascript jquery html alert

在我的first javascriptalerts text box check empty submitting second javascript confirm submit?之前<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> 然后在confirm submit我正在显示check的提醒。但是如何将这两个作为一个javascript代码?

<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>

在填写了类<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> 的文本框后显示{{1}}警告的javascript

{{1}}

HTML

{{1}}

1 个答案:

答案 0 :(得分: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;
            }
        }
     });

}