【问题标题】:how to combine two scripts如何组合两个脚本
【发布时间】:2015-10-19 22:41:45
【问题描述】:
// ===========for checking the size of an image
$(document).on('change', '#images', function() {
    files = this.files;
    size = files[0].size;

  //max size 50kb => 50*1000
    if (size < 1000141) {
        return true;

        //break;
        // end;
    }
    alert('File size greater than 1MB cannot be uploaded');
    return false;
});

//===========used for showing the preview of image
function readURL(input) {

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function(e) {
            $('#blah').attr('src', e.target.result);
            $('#blah').show();
        }
        reader.readAsDataURL(input.files[0]);
    }
}
$("#images").change(function() {
    readURL(this);
});

第一个脚本显示警报,但第二个脚本在大小大于 1MB 时提供预览。

【问题讨论】:

标签: javascript jquery ajax codeigniter


【解决方案1】:

你可以传递你的鼠标事件

onmouseover='firstscriptFuncname''second script func name'

喜欢这个

onmouseover='readURL(input) readURL(input)'

不要用逗号分隔,一个空格就够了。

【讨论】:

  • $(document).on('change', '#images', function() 如果我想使用 onmouseover='readURL(input) readURL(input)' 而不是 'change '
【解决方案2】:

DEMO

我认为你可以做这样的事情。还有其他方法可以帮助您实现这一目标。

// ===========for checking the size of an image
$(document).on('change', '#images', function() {
    files = this.files;
    size = files[0].size;
    //max size 50kb => 50*1000
    if (size < 1000141) {
        readURL(this);
        return true;
    }
    alert('File size greater than 1MB cannot be uploaded');
    return false;
});

【讨论】:

  • alert后显示图片的名称

  • 这是我上传图片的表单
猜你喜欢
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 2016-06-19
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 2011-03-31
  • 2021-04-05
相关资源
最近更新 更多