javascript - 将重复的 JQuery 代码转换为干净的代码

标签 javascript jquery html css

所以我有这段代码,但我觉得它太重复了,有人知道我该怎么做吗?上传新图片时,我希望显示一个新图片框。我的解决方案有效,但如果我想要 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'>

您还可以使用通配符选择器,其中 idimage 开头

$('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/

相关文章:

javascript - 如何使用 JS 将元素与窗口底部对齐

javascript - 当用户重新加载页面时删除 url 中的查询字符串

javascript - perl cgi 脚本无法发布到站点 - 独立工作但在 apache 下运行时失败

javascript - jQuery UI - 多个自动完成 - 结果不一致

jquery - AngularJS - Bootstrap Datepicker - 格式设置为 'mm-yyyy' 但允许用户以 'mm-yy' 格式输入日期

html - 如何停止搜索引擎索引维护页面

javascript - 按 Enter 键触发与 HTML 按钮相关的 JavaScript 函数。 (不使用Tab键)

javascript - 如果我不将上下文参数传递给 React 中的 super 键会怎样?

javascript - 重新加载数据使数据堆栈和重复与.append - Jquery

html - 如何使用css拆分一行中的文本