I need to add the text type input with the checkbox type input, at this moment when clicking on the checkbox it is adding and showing the total, but I need it to also add the text type input called service price.
This is a screenshot of my form:
This is my code:
<?php foreach($servicelist as $listsv): ?>
<input name="service[]" type="checkbox" onClick="if (this.checked) sumar(<?php echo $listsv["price"]; ?>); else restar(<?php echo $listsv["price"]; ?>)" value='<?php echo $listsv["id"]; ?>'><?php echo utf8_encode($listsv['service']); ?><br>
<?php endforeach; ?>
<br>
this is my function
var total = 0;
function sumar(valor) {
total += valor;
document.formulario.total.value = '$' + total.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
function restar(valor) {
total -= valor;
document.formulario.total.value = '$' + total.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
function otro_servicio() {
$("#lineas").append('<label>Nombre Servicio</label>');
$("#lineas").append('<input name="othersv" type="text" class="form-control importe_linea" placeholder="Nombre del servicio extra"/><br/>');
$("#lineas").append('<label>Precio Servicio</label>');
$("#lineas").append('<input name="price" type="text" class="form-control importe_linea" placeholder="Precio del servicio"/><br/>');
}
To achieve this goal I can use javascript, jquery or angular.
You could save the price of the checkbox elements as an attribute of these elements and then add a listener to the click of the checkboxes and the keyup of the input.
You can use a jquery/javascript function like this.
You can add the function in the onclick event of all price inputs
And the Service Price input can take the result of the total
I hope and serve you :)
If you want to use javascript, this may be an option:
What the following code does is described like this:
Every time the state of the
CheckBox
changes, it will check the value of the sum that exists at the moment and if it is checked , it will add the value of the check to the general sum, otherwise, it subtracts that value from the check.This is the code: