[英]Why won't my bootstrap modal close when trying to preview a picture that's about to be uploaded?
Why won't my modal close upon clicking the x
or close
? 为什么单击x
或close
时我的模态不close
? It makes absolutely no sense to me why this won't work given the right attributes that I'm using. 对于我来说,绝对没有道理,如果考虑到我正在使用的正确属性,为什么这将不起作用。
Also, when I click Preview Image
button, I can view the picture inside the modal successfully but only AFTER I close the box which asks me to upload a picture. 另外,当我单击“ Preview Image
按钮时,我可以成功查看模态中的图片,但只有在我关闭要求我上传图片的框之后,才能查看该图片。 I don't want that box to be there everytime the user clicks Preview Image
? 我不希望用户每次单击“ Preview Image
”时都存在该框吗?
Here's my code: 这是我的代码:
import React, {Component} from 'react';
import $ from 'jquery';
class Upload extends Component {
constructor(props) {
super(props);
this.state = {
selectedFile: null,
prevImgURL: '',
imgPrev: false
};
this.imageChange = this.imageChange.bind(this);
}
fileUpload() {
if ($("#new_post_image").click()) {
this.setState({imgPrev: true});
}
}
imageChange(e) {
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = () => {
this.setState({
selectedFile: file,
prevImgURL: reader.result
});
}
if (file) reader.readAsDataURL(file);
}
render() {
return (
<div>
<input
id="new_post_image"
type="file"
onChange={this.imageChange}
name="image"
accept="image/*"
/>
<button
type="button"
onClick={this.fileUpload.bind(this)}
className="btn btn-info btn-lg"
data-toggle="modal"
data-target="#myModal"
>
Preview Image
</button>
{this.state.imgPrev ?
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" data-dismiss="modal">×</button>
<h4 className="modal-title">Preview</h4>
</div>
<div className="modal-body">
<img class="img-responsive" src={this.state.prevImgURL}/>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
: null}
</div>
);
}
}
export default Upload;
Instead this : 相反,这:
fileUpload() {
if ($("#new_post_image").click()) {
this.setState({imgPrev: true});
}
Try to send argument in function directly: 尝试直接在函数中发送参数:
fileUpload = (newPostImageBool) => this.setState({imgPrev: newPostImageBool})
Also, by using es6, you dont need binding 另外,通过使用es6,您不需要绑定
You can then call it like this: 然后可以这样称呼它:
<button
type="button"
onClick={()=>this.fileUpload(true)}
className="btn btn-info btn-lg"
data-toggle="modal"
data-target="#myModal"
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.