2015-11-07 86 views
2
$(document).ready(function() { 
    $('.upld').bind('change', function() { 
     var size = this.files[0].size; 
     if (size > 1048576) { 
      $('.file_upload_error').fadeIn(200).delay(5000).fadeOut(); 
      return false; 
     } 
    }); 

    //logo width and height check 
    $('.test').bind('change', function() { 
     if (file = this.files[0]) { 
      img = new Image(); 
      img.onload = function() { 
       //alert("Width and height" + this.width+this.height); 
       if (this.width != 337 && this.height != 111) { 
        $('.logo_upload_error').fadeIn(200).delay(5000).fadeOut(); 
        return false; 
       } 
      }; 
      img.src = window.URL.createObjectURL(file); 
     } 
    }); 

    $("#vendor_registerationfrm").submit(function(e){ 
    e.preventDefault(); 
    }); 

    //banner width and height check 
    $('.banner').bind('change', function() { 
     if (file = this.files[0]) { 
      img = new Image(); 
      img.onload = function() { 
       //alert("Width and height" + this.width+this.height); 
       if (this.width != 1423 && this.height != 249) { 
        $('.banner_upload_error').fadeIn(200).delay(5000).fadeOut(); 
        return false; 
       } 
      }; 
      img.src = window.URL.createObjectURL(file); 
     } 
    }); 
}); 

上面的jquery代碼用於驗證上傳文件的大小,正在上傳的圖像的寬度和高度尺寸。上面的腳本在錯誤大小和錯誤的寬度和高度尺寸文件上傳時正在工作。但當上傳大小錯誤,寬度錯誤,高度錯誤的文件時,點擊提交按鈕,會提交大小錯誤,高度錯誤,寬度錯誤的文件。我用防止默認,但它不工作。誰能幫忙?Jquery Submit button提交不工作

<?php echo form_open_multipart(base_url() . 'crm/vendor_add_action', array('id' => 'vendor_registerationfrm')); ?> 

<!-- ... --> 

<div class="box-footer"> 
<input type="hidden" name="form_token" value="<?php echo     $this->session->userdata('form_token'); ?>" /> 
<button type="submit" class="btn btn-primary">Submit</button> 
</div> 

</div> 

</div><!-- /.box-header --> 

<!-- form start --> 
<?php echo form_close(); ?> 

以上代碼用於提交註冊表單。 jquery已經爲此註冊表單編寫。

誰能告訴我如何解決這個問題嗎?

+0

哪裏是'$( 「#vendor_registerationfrm」)上( 「提交」,功能(E){É .preventDefault(); ...})'? – mplungjan

+0

我已經寫了代碼,但它不工作... – anumol

+0

我已經包含代碼,但它不工作.. – anumol

回答

1

從處理程序返回false是毫無意義和毫無意義的,它沒有任何用處。

你應該設置一些全局標誌,並在提交表單處理程序檢查標誌:

$(document).ready(function() { 
    var _validImage = false; 

    $('.upld').bind('change', function() { 
     var size = this.files[0].size; 
     if (size > 1048576) { 
      $('.file_upload_error').fadeIn(200).delay(5000).fadeOut(); 
      _validImage = false; 
     } else { 
      _validImage = true; 
     } 
    }); 

    //logo width and height check 
    $('.test').bind('change', function() { 
     if (file = this.files[0]) { 
      img = new Image(); 
      img.onload = function() { 
       //alert("Width and height" + this.width+this.height); 
       if (this.width != 337 && this.height != 111) { 
        $('.logo_upload_error').fadeIn(200).delay(5000).fadeOut(); 
        _validImage = false; 
       } else { 
        _validImage = true; 
       } 
      }; 
      img.src = window.URL.createObjectURL(file); 
     } 
    }); 


    $("#vendor_registerationfrm").submit(function(e){ 
     if (!_validImage) { 
      //image is invalid, don't submit 
      e.preventDefault(); 
     } 
    }); 
}); 
+0

非常感謝你...它的工作...:D – anumol