2012-08-13 64 views
0

我必須在一個servlet中添加4個函數,如添加,刪除,更新,搜索。我正在使用jQuery來驗證我的表單。所以,我怎樣才能把jQuery中的單獨按鈕操作。我在下面添加了我的驗證腳本。任何一個可以請幫我做這件事......使用jQuery的多個按鈕動作

$(document).ready(function() { 
    $("#departmentId").keypress(function (e) 
    { 
     //if the letter is not digit then display error and don't type anything 
     if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) 
     { 
      //display error message 
      $("#errmsg").html("Digits Only").show().fadeOut("slow"); 
      return false; 
     } 
    }); 


    $('.btn-delete').click(function(e){ 

     // Declare the function variables: 
     // Parent form, form URL, email regex and the error HTML 
     var $formId = $(this).parents('form'); 
     var formAction = $formId.attr('action'); 

     var $error = $('<span class="error"></span>'); 

     // Prepare the form for validation - remove previous errors 
     $('li',$formId).removeClass('error'); 

     $('span.error').remove(); 

     // Validate all inputs with the class "required" 
     $('.required',$formId).each(function(){ 
      var inputVal = $("#departmentId").val(); 
      var $parentTag = $("#departmentId").parent(); 
      if(inputVal == ''){ 
       $parentTag.addClass('error').append($error.clone().text('Required Field')); 
      } 






      // Run the email validation using the regex for those input items also having class "email" 


      // Check passwords match for inputs with class "password" 


     }); 

     // All validation complete - Check if any errors exist 
     // If has errors 



     if ($('span.error').length > 0) { 

      $('span.error').each(function(){ 

       // Set the distance for the error animation 
       var distance = 5; 

       // Get the error dimensions 
       var width = $(this).outerWidth(); 

       // Calculate starting position 
       var start = width + distance; 

       // Set the initial CSS 
       $(this).show().css({ 
        display: 'block', 
        opacity: 0, 
        right: -start+'px' 
       }) 
       // Animate the error message 
       .animate({ 
        right: -width+'px', 
        opacity: 1 
       }, 'slow'); 

      }); 
     } else { 
      $formId.submit(); 
     } 
     // Prevent form submission 
     e.preventDefault(); 


    }); 

    // Fade out error message when input field gains focus 
    $('.required').focus(function(){ 
     var $parent = $(this).parent(); 
     $parent.removeClass('error'); 
     $('span.error',$parent).fadeOut(); 
    }); 

    $('.btn-submit').click(function(e){ 

     // Declare the function variables: 
     // Parent form, form URL, email regex and the error HTML 
     var $formId = $(this).parents('form'); 
     var formAction = $formId.attr('action'); 
     var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
     var $error = $('<span class="error"></span>'); 

     // Prepare the form for validation - remove previous errors 
     $('li',$formId).removeClass('error'); 

     $('span.error').remove(); 

     // Validate all inputs with the class "required" 
     $('.required',$formId).each(function(){ 
      var inputVal = $(this).val(); 
      var $parentTag = $(this).parent(); 
      if(inputVal == ''){ 
       $parentTag.addClass('error').append($error.clone().text('Required Field')); 
      } 






      // Run the email validation using the regex for those input items also having class "email" 


      // Check passwords match for inputs with class "password" 


     }); 

     // All validation complete - Check if any errors exist 
     // If has errors 



     if ($('span.error').length > 0) { 

      $('span.error').each(function(){ 

       // Set the distance for the error animation 
       var distance = 5; 

       // Get the error dimensions 
       var width = $(this).outerWidth(); 

       // Calculate starting position 
       var start = width + distance; 

       // Set the initial CSS 
       $(this).show().css({ 
        display: 'block', 
        opacity: 0, 
        right: -start+'px' 
       }) 
       // Animate the error message 
       .animate({ 
        right: -width+'px', 
        opacity: 1 
       }, 'slow'); 

      }); 
     } else { 
      $formId.submit(); 
     } 
     // Prevent form submission 
     e.preventDefault(); 


    }); 

    // Fade out error message when input field gains focus 
    $('.required').focus(function(){ 
     var $parent = $(this).parent(); 
     $parent.removeClass('error'); 
     $('span.error',$parent).fadeOut(); 
    }); 



}); 

回答

0

請參考以下鏈接來清除相關的提交按鈕,所有的概念..

link

希望你有解決方案。