所以我有这段代码,但我觉得它太重复了,有人知道我该怎么做吗?上传新图片时,我希望显示一个新图片框。我的解决方案有效,但如果我想要 1000 张新图像怎么办?我不能一次只打一个字。我可以做些什么来优化这个问题?
HTML:
<div class="wrapper">
<div class="box box_image" id="box_image_1">
<div class="js--image-preview"></div>
<div class="upload-options">
<label>
<input type="file" class="image-upload" id="image1" data-show='box_image_2' name="IgniteFormObject.Image1" accept="image/*" enctype="multipart/form-data" />
</label>
</div>
</div>
<div class="box box_image" id="box_image_2">
<div class="js--image-preview"></div>
<div class="upload-options">
<label>
<input type="file" class="image-upload" id="image2" data-show='box_image_3' name="IgniteFormObject.Image2" accept="image/*" enctype="multipart/form-data" />
</label>
</div>
</div>
<div class="box box_image" id="box_image_3">
<div class="js--image-preview"></div>
<div class="upload-options">
<label>
<input type="file" class="image-upload" id="image3" data-show='box_image_4' name="IgniteFormObject.Image3" accept="image/*" enctype="multipart/form-data" />
</label>
</div>
</div>
<div class="box box_image" id="box_image_4">
<div class="js--image-preview"></div>
<div class="upload-options">
<label>
<input type="file" class="image-upload" id="image4" data-show='box_image_5' name="IgniteFormObject.Image4" accept="image/*" enctype="multipart/form-data" />
</label>
</div>
</div>
<div class="box box_image" id="box_image_5">
<div class="js--image-preview"></div>
<div class="upload-options">
<label>
<input type="file" class="image-upload" id="image5" name="IgniteFormObject.Image5" accept="image/*" enctype="multipart/form-data" />
</label>
</div>
</div>
</div>
Javascript:
上传图片时,调用此 JQuery。
$('#image1').change(function (ev) {
$("#box_image_2").show();
});
$('#image2').change(function (ev) {
$("#box_image_3").show();
});
$('#image3').change(function (ev) {
$("#box_image_4").show();
});
$('#image4').change(function (ev) {
$("#box_image_5").show();
});
最佳答案
您可以使用data
属性和多重选择器。在数据属性中传递要显示的元素的 id
并在更改时获取该属性
$('#image1,#image2,#image3,#image4').on('change', function(ev) {
let toShow = $(this).data('show');
console.log(toShow);
// $('#'+toShow).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id='image1' type='checkbox' data-show='box_image_2'>
<input id='image2' type='checkbox' data-show='box_image_3'>
<input id='image3' type='checkbox' data-show='box_image_4'>
<input id='image4' type='checkbox' data-show='box_image_5'>
您还可以使用通配符选择器,其中 id
以 image
开头
$('input[id^="image"]').on('change', function(ev) {
let toShow = $(this).data('show');
console.log(toShow);
// $('#'+toShow).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id='image1' type='checkbox' data-show='box_image_2'>
<input id='image2' type='checkbox' data-show='box_image_3'>
<input id='image3' type='checkbox' data-show='box_image_4'>
<input id='image4' type='checkbox' data-show='box_image_5'>
关于javascript - 将重复的 JQuery 代码转换为干净的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55070671/