JavaScript - configurar boton de eliminar antes de subir una imagen

 
Vista:

configurar boton de eliminar antes de subir una imagen

Publicado por Christopher Olivares (5 intervenciones) el 06/07/2016 01:43:04
Esque he estado jugando un poco con javascript, y he conseguido hasta que me elimine el div donde se precarga el archivo pero no se como lo hago para eliminar la seleccion de este porque arriba sigue diciendo que tengo la misma cantidad de archivos seleccionados y los sube todos. ayuda por favor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<script>
 
$(function(){
var mos = document.getElementById('mostrador');
$("#file").on("change", function(){
 
	/* Limpiar vista previa */
	$("#mostrador").html('');
	var archivos = document.getElementById('file').files;
	var navegador = window.URL || window.webkitURL;
	/* Recorrer los archivos */
	for(x=0; x<archivos.length; x++)
	{
			var num = $('.previa').length;
			var nnum = new Number(num +1);
 
		/* Validar tamaño y tipo de archivo */
		var type = archivos[x].type;
		var name = archivos[x].name;
 
		if(type != 'image/jpeg' && type != 'image/jpg' && type != 'image/png' && type != 'image/gif')
		{
			var base = document.getElementById('d' + num);
			var nuevo = document.createElement('div');
			nuevo.setAttribute('id','vista-previa' + nnum);
			var np = document.createElement('p');
			np.setAttribute('style','color:red;');
			np.setAttribute('title','imagen no permitida');
			nuevo.appendChild(np);
			mos.insertBefore(nuevo, base);
		}
		else
		{ var objeto_url = navegador.createObjectURL(archivos[x]);
			var base = document.getElementById('d' + num);
			var nuevo = document.createElement('div');
			nuevo.setAttribute('id','vista-previa' + nnum);
			nuevo.setAttribute('class', 'previa');
			nuevo.setAttribute('style', 'display: inline-block; margin: 2px;');
			var ni = document.createElement('img');
			ni.setAttribute('src', objeto_url);
			ni.setAttribute('width', '150');
			ni.setAttribute('height', 'auto');
			var nb = document.createElement('button');
			nb.setAttribute('id', 'btnBorrar');
			nb.setAttribute('type', 'button');
			nb.setAttribute('name', 'vista-previa' + nnum);
			nb.setAttribute('onClick', 'borrar(this.name)' );
			nb.setAttribute('style', 'left: 0;');
			var ib = document.createElement('img');
			ib.setAttribute('src', 'imagen/icon_eli.png');
			ib.setAttribute('width', '15');
			ib.setAttribute('height', 'auto');
			nb.appendChild(ib);
			nuevo.appendChild(ni);
			nuevo.appendChild(nb);
			mos.insertBefore(nuevo, base);
		}
	}
});
 
 
$("#btn").on("click", function(){
	var formData = new FormData($("#formulario")[0]);
	var ruta = "multiple-ajax.php";
	$.ajax({
		url: ruta,
		type: "POST",
		data: formData,
		contentType: false,
		processData: false,
		success: function(datos)
		{
			$("#respuesta").html(datos);
		}
	});
	});
 
});
function borrar(div){
var fi = document.getElementById('mostrador');
fi.removeChild(document.getElementById(div));
}
 
</script>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder