下面的示例是有效的,并且工作正常,但是它太长了,并且经常重复相同的事情。(我必须这样做,因为我可能不会使用多重上传属性- HTML5 -)。用户应该能够上传几个文件(最多5个),每次通过一个新的输入文件元素。他们只有在必要时才会提出上诉。
这是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/>这是jQuery-部分:
$('#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();
});我怎样才能缩短这个时间?
发布于 2013-10-24 11:58:54
您可以使用如下内容:(用于表演部分)
$('[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();
});我使用begin with选择器jQuery
将此想法用于删除部分
$('[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();
});https://stackoverflow.com/questions/19565281
复制相似问题