如何使用javascript中的所有其他输入类型计算无线电输入值?
这是我的密码如何使用javascript中的所有其他输入类型计算无线电输入值?,javascript,jquery,Javascript,Jquery,这是我的密码 <div id="estimate"> <div class="form-group"> <div class="range-slider"> <input class="range-slider__range form-control prc e-range" type="range&q
<div id="estimate">
<div class="form-group">
<div class="range-slider">
<input class="range-slider__range form-control prc e-range" type="range" value="100" min="0" max="500">
<span class="range-slider__value">0</span>
</div>
</div>
<div class="form-group">
<input type="number" class="form-control prc" value="{{vl}}"/>
</div>
<div class="form-group">
<input type="number" class="form-control prc" value="5"/>
</div>
<div class="form-group">
<input type="radio" value="10" class="form-control prc">
<input type="radio" value="20" class="form-control prc">
<input type="radio" value="70" class="form-control prc">
</div>
<p id="result"></p>
</div>
<script>
estimate();
$('.form-group').on('change','.prc',function(){
estimate();
});
function estimate(){
let totalSum = 0;
$('.form-group .prc').each(function(){
let inputVal = $(this).val();
if($.isNumeric(inputVal)){
totalSum += parseFloat(inputVal);
}
});
$('#result').html(totalSum);
}
var rangeSlider = function(){
var slider = $('.range-slider'),
range = $('.range-slider__range'),
value = $('.range-slider__value');
slider.each(function(){
value.each(function(){
var value = $(this).prev().attr('value');
$(this).html(value);
});
range.on('input', function(){
$(this).next(value).html(this.value);
});
});
};
rangeSlider();
</script>
0
估算();
$('.form group').on('change','.prc',function(){
估算();
});
函数估计(){
设totalSum=0;
$('.form group.prc')。每个(函数(){
让inputVal=$(this.val();
如果($.isNumeric(inputVal)){
totalSum+=parseFloat(inputVal);
}
});
$('#result').html(totalSum);
}
var rangeSlider=函数(){
变量滑块=$(“.范围滑块”),
范围=$('.range-slider__range'),
值=$('.range-slider__值');
slider.each(函数(){
value.each(函数(){
var value=$(this.prev().attr('value');
$(this.html(value);
});
range.on('input',function(){
$(this.next(value.html)(this.value);
});
});
};
范围滑块();
没有单选按钮计算器工作正常,但作为单选按钮,如何使用相同的方法计算这些单选按钮值。。就像动态场一样。。无法使用我可以通过id调用的任何静态id。。每个项目都必须调用它。…解决问题的一种方法是插入一个
if(this.type=="radio" && !this.checked) return
进入您的estimate()
函数
estimate1();
美元('form group')。在('change','prc',estimate1)上;
函数估计1(){
$(“#结果”)
.html($('.form group.prc').get()
.reduce((a,c)=>((c.type!=“radio”| | c.checked)&(a+=+c.value),a),0))
}
函数估计(){
设totalSum=0;
$('.form group.prc')。每个(函数(){
if(this.type==“radio”&&!this.checked)返回
让inputVal=$(this.val();
如果($.isNumeric(inputVal)){
totalSum+=parseFloat(inputVal);
}
});
$('#result').html(totalSum);
}
var rangeSlider=函数(){
变量滑块=$(“.范围滑块”),
范围=$('.range-slider__range'),
值=$('.range-slider__值');
slider.each(函数(){
value.each(函数(){
var value=$(this.prev().attr('value');
$(this.html(value);
});
range.on('input',function(){
$(this.next(value.html)(this.value);
});
});
};
范围滑块()代码>
0
10
20
70
您可以使用以下代码执行您想要的操作,您需要检查当前获取值的输入是否为无线电
,然后检查是否为无线电
我有一个工作演示,在那里我把它添加到了代码中的相关位置
请记住为收音机添加标签,以便用户了解其价值。有关正确的HTML格式,请参阅
// Check if radio button and checked
if ($(this).attr('type') == "radio" && $(this).prop('checked')==false){
// If not change value to 0
inputVal = 0;
}
estimate();
$('.form group').on('change','.prc',function(){
估算();
});
函数估计(){
设totalSum=0;
$('.form group.prc')。每个(函数(){
让inputVal=$(this.val();
//检查单选按钮是否已选中
if($(this).attr('type')==“无线电”&&
$(this.prop('checked')==false){
//如果不是,请将值更改为0
inputVal=0;
}
控制台日志(inputVal);
如果($.isNumeric(inputVal)){
totalSum+=parseFloat(inputVal);
}
});
$('#result').html(totalSum);
}
var rangeSlider=函数(){
变量滑块=$(“.范围滑块”),
范围=$('.range-slider__range'),
值=$('.range-slider__值');
slider.each(函数(){
value.each(函数(){
var value=$(this.prev().attr('value');
$(this.html(value);
});
range.on('input',function(){
$(this.next(value.html)(this.value);
});
});
};
范围滑块()代码>
0