How to calculate values from input type="text" and select element with jquery?
How do I calculate fields in a form with both input type and select element with jQuery?
For example
<input class="days" type="text" name="txt"/>
<select class="roomtype">
<option value="">Please select a room</option>
<option value="50">Budget room</option>
<option value="100">Standard room</option>
</select>
<p>Please bank in <a href="bankurl" id="total">$</a> to our account.
The script that I have currently ask the users to enter their price.
$(".days").each(function() {
$(this).keyup(function(){
Calculate();
});
});
});
function Calculate() {
var total = 0;
$(".days").each(function() {
// validate
if(!isNaN(this.value) && this.value.length!=0) {
开发者_StackOverflowtotal += parseFloat(this.value);
}
});
//decimal
$("#total").html(total.toFixed(2));
}
Thank you.
Try this solution:
$(".days").each(function() {
$(this).keyup(function(){
Calculate();
});
});
function Calculate() {
var total = 0;
$(".days").each(function() {
// validate
if(!isNaN(this.value) && this.value.length!=0) {
total += parseFloat(this.value);
}
});
//decimal
var cost = total.toFixed(2) * $(".roomtype").val();
cost += "$";
$("#total").html(cost);
}
jsFiddle: http://jsfiddle.net/wALQT/
You can get select
's value using $('.roomtype').val()
.
精彩评论