calculate only selected input text with checkbox
Hi I need to calculate the sum from m开发者_运维问答ultiple selected input text form, that are selected with checkbox like this:
<input type="text" value="" name="satelitep">
<input type="checkbox" value="sateliteon" name="satelite">
How can i do this whit JavaScript or jQuery?
Thanks
You can select them and get the size:
var score = $("input:checkbox[checked=true]").size();
Try something like
var sum = 0;
$("input:checkbox [name='satelite']:checked").each(function(){
sum += parseFloat($(this).prev().val());
});
Something like this should do the job.
var counter = 0;
$('input:checked').prev().each(function(index){
counter += parseFloat(this.val());
});
It is untested, but should show the basic idea. It requires jQuery.
This finds all checked
input fields, finds the fields immediately preceding them, then loops over each of them, adding their value to a counter.
Hope this helps
精彩评论