Javascript 函数外的复选框变量
我不熟悉编码(尝试学习),我不知道如何在函数之外获取复选框值的var Javascript:Javascript 函数外的复选框变量,javascript,jquery,Javascript,Jquery,我不熟悉编码(尝试学习),我不知道如何在函数之外获取复选框值的var Javascript: $(document).ready(function() { var quantity= parseInt($('#phones').val()); $("#check1 input:checkbox").change(function() { var feature = 0; $("#check1 input:checkbox").each(function() {
$(document).ready(function() {
var quantity= parseInt($('#phones').val());
$("#check1 input:checkbox").change(function() {
var feature = 0;
$("#check1 input:checkbox").each(function() {
if ($(this).is(':checked')) {
feature += parseInt($(this).prop('value'));
}
});
});
var grand = feature * (quantity * Number ('0.1'))
var total = quantity + grand
});
HTML:
<input id="phones" type="numerical" value="0" style="text-align: right"/>
<div id="check1">
<input type="checkbox" value="1" />
函数getResult(功能,手机){
if(feature)你的确切意思是什么?有什么特殊原因不能将所有变量和数学值保留在事件处理程序中吗?有多个复选框,每个复选框的默认值为0,选中时=1。复选框的值乘以一个等式(每个复选框有不同的等式)所有复选框的结果和产品的数量相加为一个总数。parseInt('0.1')是0。因此grand是0,total=quantity对不起,这应该是数字('0.1')。我已经更正了。
function getResult(feature, phones) {
if (feature <= 0 || phones <= 0) {
return 'Error...., enter the number of phones and check some checkbox';
}
return (feature * (+phones * 0.1)) + +phones;
}
function getFeature() {
var feature = 0;
$('#check1 input:checkbox:checked').each(function () {
feature += +$(this).prop('value') || 1;
});
return feature;
}
$(document).ready(function() {
var $phones = $('#phones'),
$result = $('#result');
$("#check1 input:checkbox").change(function() {
$result.html(getResult(getFeature(), $phones.val()));
});
$("#phones").keyup(function() {
$result.html(getResult(getFeature(), $phones.val()));
});
});