开发者

calculate only selected input text with checkbox

开发者 https://www.devze.com 2023-02-16 13:29 出处:网络
Hi I need to calculate the sum from m开发者_运维问答ultiple selected input text form, that are selected withcheckbox like this:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消