Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
¿Cómo puedo hacer que mi formulario HTML convierta un número positivo en uno negativo?

Actualmente tengo el siguiente formulario HTML;

 <form method = "POST" action = "https://script.google.com/macros/s/AKfycbxjxbUoJes8WFs1ybMpsnYbaiJcFI1PkVFf0y0avSdKmsxtys7LKi6Zdr7tmWyw/exec" id = "groceryExpenses" style = "display:none" onsubmit='addExpense(); checkIfExpense();'> <div class="row"> <div> <label>Car Name</label> <input name = "Car" id = 'carName' style = "width: 24%;" type="text" placeholder="Enter the car name" id="exampleEmailInput"> </div> </div> <div> <div> <label style = 'padding-bottom: 1pt;' for="exampleEmailInput">Gas Station</label> <input name = "Gas Station" id="gasStation" style = "width: 24%;" type="text" placeholder="Enter the gas station" id="exampleEmailInput"> </div> <label>Amount</label> <input name = "Amount" id="amount" style = "width: 24%;" type="number" placeholder="Enter amount spent" id="exampleEmailInput"> </div> <button type = 'submit' class="button-primary">Add expense</button> <button type = 'button' class="button-primary" onclick="closeExpenseForm()">Close Form</button> </form>

Tras el envío, me gustaría comprobar si las entradas de carName gasStation están vacías. Si no los hay, la amount se convertirá en un número negativo. He probado lo siguiente;

 function checkIfExpense(){ if (document.getElementById('gasStation').value.length !==0 && document.getElementById('carName').value.length !==0){ amount = Math.abs(amount)*-1; //I have instantiated this variable earlier in my JS file. } }

Sin embargo, no parece estar funcionando. ¿Cómo puedo conseguir esto?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

establece esa cantidad negativa usando:

 document.getElementById('amount').value = amount

Prueba esto:

 function checkIfExpense(e) { const gasStation = document.getElementById('gasStation').value; const carName = document.getElementById('carName').value; let amount = document.getElementById('amount').value; if (!gasStation || !carName || gasStation == null || carName == null || gasStation == '' || carName == ''){ amount = amount ? Math.abs(amount)*-1 : 0; console.log(amount); document.getElementById('amount').value = amount; } }
 <div> <label>Car Name</label> <input name="Car" id='carName' style="width: 24%;" type="text" placeholder="Enter the car name" id="exampleEmailInput"> </div> <div> <label style = 'padding-bottom: 1pt;' for="exampleEmailInput">Gas Station</label> <input name="Gas Station" id="gasStation" style = "width: 24%;" type="text" placeholder="Enter the gas station" id="exampleEmailInput"> </div> <div> <label>Amount</label> <input name="Amount" id="amount" style="width: 24%;" type="number" placeholder="Enter amount spent" id="exampleEmailInput"> </div> <button type='submit' onclick="checkIfExpense()" class="button-primary">Submit</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

No estoy seguro de lo que estás tratando de lograr. Pero también puedes hacer esto...

 function checkIfExpense(){ if (document.getElementById('gasStation').value.length !==0 && document.getElementById('carName').value.length !==0){ var amount = document.getElementById('amount'); amount = amount.value; alert(-amount);

} }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda