简体   繁体   English

jQuery示例:如何有效地缩短此代码?

[英]jQuery Example: How to shorten this code efficiently?

Following example is valid and works properly, but it is too long and the same things are repeated often. 以下示例有效并且正常工作,但它太长并且经常重复相同的事情。 (I've to do it on this way, because I may not use multiple-upload attribute - HTML5 -). (我要这样做,因为我可能不会使用多重上传属性 - HTML5 - )。 User should be able to upload several files (up to 5) and each time via a new input-file-element. 用户应该能够通过新的input-file-element上传多个文件(最多5个)。 They should only appeare, if necessary. 如有必要,他们应该只出现。

This is the HTML-Part: 这是HTML部分:

<input type="file" id="image1" class="fileImage">
    <button class="cShow" id="show_i2">+1</button><br/>

<input type="file" id="image2" class="fileImage">
    <button class="cShow" id="show_i3">+1</button>
        <button class="cDel" id="del_i2">Delete</button><br/>

<input type="file" id="image3" class="fileImage">
    <button class="cShow" id="show_i4">+1</button>
        <button class="cDel" id="del_i3">Delete</button><br/>

<input type="file" id="image4" class="fileImage">
    <button class="cShow" id="show_i5">+1</button>
        <button class="cDel" id="del_i4">Delete</button><br/>

<input type="file" id="image5" class="fileImage">
    <button class="cDel" id="del_i5">Delete</button><br/>

This is jQuery-Part: 这是jQuery-Part:

$('#show_i2').click(function(event) {
    $('#image2, #show_i3, #del_i2').show();
    $('#show_i2').hide();
    event.preventDefault();
});

$('#show_i3').click(function(event) {
    $('#image3, #del_i3, #show_i4').show();
    $('#show_i3, #del_i2').hide();

    event.preventDefault();
});

$('#show_i4').click(function(event) {
    $('#image4, #del_i4, #show_i5').show();
    $('#show_i4, #del_i3').hide();

    event.preventDefault();
});

$('#show_i5').click(function(event) {
    $('#image5, #del_i5').show();
    $('#show_i5, #del_i4').hide();
    event.preventDefault();
});


$('#del_i2').click(function(event) {
    $('#image2, #del_i2, #show_i3').hide();
    $('#show_i2').show();
    event.preventDefault();

});

$('#del_i3').click(function(event) {
    $('#image3, #del_i3, #show_i4').hide();
    $('#show_i3, #del_i2').show();
    event.preventDefault();
});

$('#del_i4').click(function(event) {
    $('#image4, #del_i4, #show_i5').hide();
    $('#show_i4, #del_i3').show();
    event.preventDefault();
});

$('#del_i5').click(function(event) {
    $('#image5, #del_i5').hide();
    $('#show_i5, #del_i4').show();
    event.preventDefault();
});

How can I shorten this? 我怎样才能缩短这个?

You can use something like this : (for show part) 您可以使用以下内容:(用于显示部分)

$('[id^="show_i"]').click(function(event) {
    var index = $(this).attr(id).replace('show_i', '').parseInt();
    $('#image'+index+', #del_i'+index+', #show_i'+(index+1)).show();
    $('#show_i'+index+', #del_i'+(index-1)).hide();

    event.preventDefault();
});

I use the begin with selector of jQuery 我使用jQuerybegin with selector

Use this idea for the delete part 将此想法用于删除部分

$('[id^="del_i"]').click(function(event) {
    var index = $(this).attr(id).replace('del_i', '').parseInt();
    $('#image'+index+', #del_i'+index+', #show_i'+(index+1)).hide();
    $('#show_i'+index).show();
    event.preventDefault();

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM