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?
establece esa cantidad negativa usando:
document.getElementById('amount').value = amountPrueba 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>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);} }