Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Limpiar(){
- $("#txtModelo").val("");
- $("#txtMarca").val("");
- $("#txtPrecio").val("");
- $("#txtCantidad").val("");
- $("#txtDescuento").val("");
- }
- function LlenarTabla(muebles) {
- var tablaMuebles = $("#tablaMuebles");
- var html = "<tr><th>Modelo</th><th>Marca</th><th>Precio</th><th>cantidad</th><th>Descuento</th></tr>";
- muebles.forEach(function(item){
- html += "<tr><td>"+item.modelo+"</td><td>"+item.marca+"</td><td>"+item.precio+"</td><td>"+item.cantidad+"</td><td>"+item.descuento+"</td></tr>";
- });
- tablaMuebles.html(html);
- }
- $(document).ready(function(){
- var btnLimpiar = $("#btnLimpiar");
- btnLimpiar.click(function() {
- Limpiar();
- });
- var muebles = [
- {
- "modelo": "RKM-789",
- "marca" : "IKEA",
- "precio": 52300,
- "cantidad": 3,
- "descuento": 5
- },
- {
- "modelo": "FGW-165",
- "marca" : "KREA",
- "precio": 69300,
- "cantidad": 3,
- "descuento": 5
- }
- ]
- var btnGuardar = $("#btnGuardar");
- btnGuardar.click(function(){
- var txtModelo = $("#txtModelo").val();
- var txtMarca = $("#txtMarca").val();
- var txtPrecio = $("#txtPrecio").val();
- var txtCantidad = $("#txtCantidad").val();
- var txtDescuento = $("#txtDescuento").val();
- var listaErrores = $("#listaErrores").css("color","#c62828");
- var errores = [];
- if (txtModelo == "") {
- errores.push("El nombre es requerido");
- }else if(txtModelo.length < 3 || txtModelo.length >= 25){
- errores.push("El campo modelo debe ser entre 3 y 25 caracteres");
- }
- if (txtMarca == "") {
- errores.push("La marca es requerida");
- }else if(txtMarca.length < 3 || txtMarca.length >= 25){
- errores.push("El campo marca debe ser entre 3 y 10 caracteres");
- }
- if (txtPrecio == "") {
- errores.push("El precio es requerido");
- }else if(isNaN(txtPrecio)){ // verificamos que sea numero
- errores.push("El precio debe ser numerica");
- }
- if (txtCantidad == "") {
- errores.push("La cantidad es requerida");
- }else if(isNaN(txtCantidad)){
- errores.push("El cantidad debe ser numerica");
- }
- if (errores.length > 0) {
- var html = "";
- errores.forEach(function(item){
- html += "<li>"+item+"</li>";
- });
- listaErrores.html(html);
- return;
- }
- var datos = {
- "modelo": txtModelo,
- "marca" : txtMarca,
- "precio": txtPrecio,
- "cantidad": txtCantidad,
- "descuento": txtDescuento
- }
- muebles.push(datos);
- Limpiar();
- LlenarTabla(muebles);
- });
- LlenarTabla(muebles);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement