更改图像文件 javascript 的宽度
Change width of a image file javascript
如果用户选择一张图像,我会在div中将其预览为背景图像,我有2个输入字段(宽度,高度),因此用户可以确定上传图像的宽度和高度。
如果用户更改宽度/高度,我想调整预览图像的大小。
但是我一直收到错误:Uncaught TypeError: Cannot read property 'width' of undefined
这是我的代码:
HTML - 输入字段
<input type="file" name="sourceImage" id="sourceImage">
<input type="text" class="form-control input-width input-px" maxlength="2" name="width">CM breed
<input type="text" class="form-control input-height input-px" maxlength="2" name="height">CM hoog
Javascript - 函数
在这个函数中,我预览图像并将图像放入图像变量中
var image;
$(function() {
$("#sourceImage").on("change", function()
{
var files = !!this.files ? this.files : [];
if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support
if (/^image/.test( files[0].type)){ // only image file
var reader = new FileReader(); // instance of the FileReader
reader.readAsDataURL(files[0]); // read the local file
image = this.result;
reader.onloadend = function(){ // set image data as background of div
$( ".source" ).css( "background-image", "url("+this.result+")" );
}
}
});
});
在此函数中,我想更改将文件存储到的图像变量的宽度
$(function(){
$(".input-px").on("change", function()
{
image.width($(".input-width").val());
})
})
$(function(){
$(".input-px").on("change", function() {
var _width = parseInt($(".input-width").val(), 10) || 0;
var _height = parseInt($(".input-height").val(), 10) || 0;
if(_width > 0) {
image.width(_width);
}
if(_height > 0) {
image.width(_height);
}
});
});
编辑:请将其作为输入px onchange事件功能的增强功能。
相关文章:
- 使用压缩的JavaScript文件(不是运行时压缩)
- 如何包含特定于每个视图angularjs的javascript文件
- 将数据从javascript文件导入VB.Net页面
- Chrome加载旧版本的Javascript文件
- 操作员”;新的“;根据我想在几个JavaScript文件中使用的类,在JavaScript中使用
- gulp-if-javascript文件,但不在gulp-useref的特定目录中
- 如何在visualstudio中调试web api时编辑javascript文件
- 关于引入外部javascript文件的问题&css通过https
- 有没有办法在tinymceiframe中加载一个外部javascript文件
- 什么'是在asp.net MVC中将本地化的resources.resx文件转换为javascript文件的有效
- 在一个javascript文件中为整个网站创建标签
- 如何在定义js文件后为外部javascript文件设置变量
- 显示IIS上javascript文件(SOAP请求)的XML响应
- ResolveUrl没有'我似乎不适合当链接到具有动态虚拟目录的外部javascript文件时
- 我应该将MVC视图特定的javascript文件放在哪个文件夹中
- Django'支持Javascript文件中的翻译
- MVC正在忽略我的Javascript文件
- 为什么不't我的ruby代码与javascript文件一起插入
- 在Javascript文件中获取PHP变量
- 如何在Windows中将Javascript文件编译成二进制文件