Jquery “保存”;这";传递到函数并使用parent()和prev()时的位置
我有一些重复的动态数据,为了执行我的结果,我必须找到相邻的元素,比如Jquery “保存”;这";传递到函数并使用parent()和prev()时的位置,jquery,Jquery,我有一些重复的动态数据,为了执行我的结果,我必须找到相邻的元素,比如parent()和prev()。我有一个以前的问题没有得到回答,它最初与这个问题无关,但我发现它与这个问题无关,我遇到的问题是当我将this传递到一个函数parent()和prev()中时找不到正确的位置,因为我不相信此被保留 下面发生的事情是,当我使用输入选择一个图像时,它会将图像渲染到选择它的当前输入之上。相反,它总是渲染第一个输入上方的图像 下面是一个例子来说明发生了什么 我需要的是显示在我选择的输入上方的图像,而不仅仅是
parent()
和prev()
。我有一个以前的问题没有得到回答,它最初与这个问题无关,但我发现它与这个问题无关,我遇到的问题是当我将this
传递到一个函数parent()
和prev()中时
找不到正确的位置,因为我不相信此
被保留
下面发生的事情是,当我使用输入选择一个图像时,它会将图像渲染到选择它的当前输入之上。相反,它总是渲染第一个输入上方的图像
下面是一个例子来说明发生了什么
我需要的是显示在我选择的输入上方的图像,而不仅仅是显示在顶部。希望这是有道理的
HTML
<div class="form-group">
<i class="fa fa-times pointer socialmedia_image_remove" aria-hidden="true" style="display: none"></i>
<img class="img-responsive img-rounded socialmedia_image" src="" style="max-width: 100px; border: solid 1px lightgrey; display: none">
<div class="form-group">
<label for="file-upload" class="custom-file-upload"><i class="fa fa-camera" aria-hidden="true"></i></label>
<input id="file-upload" type="file" class="form-control add_image_new">
</div>
</div>
<div class="form-group">
<i class="fa fa-times pointer socialmedia_image_remove" aria-hidden="true" style="display: none"></i>
<img class="img-responsive img-rounded socialmedia_image" src="" style="max-width: 100px; border: solid 1px lightgrey; display: none">
<div class="form-group">
<label for="file-upload" class="custom-file-upload"><i class="fa fa-camera" aria-hidden="true"></i></label>
<input id="file-upload" type="file" class="form-control add_image_new">
</div>
</div>
事实上,您的输入使用相同的ID是导致问题的原因。两个标签都指向相同的输入,通过它们的
for
属性。将输入的ID更改为唯一。然后相应地更改标签的属性
下面是一个更新的事实上,您的输入使用相同的ID是导致问题的原因。两个标签都指向相同的输入,通过它们的for
属性。将输入的ID更改为唯一。然后相应地更改标签的属性
以下是更新后的ID,它们是唯一的,在当前文档中只能使用一次。只需更改您的ID和属性的,这样您就不会使用重复的ID。ID是唯一的,并且只能在当前文档中使用一次。只需更改您的ID和属性的,这样您就不会使用重复的ID。是的,我觉得自己像个白痴,我甚至没有注意到我这样做了,我知道得更清楚。修好了,伙计们,谢谢,我也这么做了,没问题。:)是的,我觉得自己像个白痴,我甚至没有注意到我做了那件事,我更清楚。修好了,伙计们,谢谢,我也这么做了,没问题。:)
function readURLNew(input) {
this.input = input;
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
debugger;
$(input).parent().prev().attr('src', e.target.result);
$(input).parent().prev().prev().show();
$(input).parent().prev().show();
}
reader.readAsDataURL(input.files[0]);
}
}
$(function() {
$(document).on('change','.add_image_new', function() {
readURLNew(this);
});
})