我有一个简单公式的脚本:
Input 1 * Input 2 * Input 3 / 25 = result
如果检查了checbox,我如何操作值25?
例如,如果选中复选框,则输入1,2,3应在末尾除以35而不是25,并显示结果除以35。
答案 0 :(得分:2)
function calculate() {
var myBox1 = document.getElementById('box1').value;
var myBox2 = document.getElementById('box2').value;
var myBox3 = document.getElementById('box3').value;
var result = document.getElementById('result');
var divide = (document.getElementById('changeValue').checked ? 35 : 25);
var myResult = myBox1 * myBox2 * myBox3 / divide;
result.value = myResult.toFixed(2) + ' eggs';
}
答案 1 :(得分:0)
更改
var myResult = myBox1 * myBox2 * myBox3 / 25;
的
var divisor = ((document.getElementById('changeValue').checked)?35:25);
var myResult = myBox1 * myBox2 * myBox3 / divisor ;
答案 2 :(得分:0)
基本检查以查看是否选中了复选框。
var isChecked = document.getElementById("changeValue").checked; //get boolean for checked
var num = isChecked ? 35 : 25; //ternary operation
参考文献:Conditional_Operator
答案 3 :(得分:0)
您可以通过添加:
来实现var value = (document.getElementById('changeValue').checked)?35:25;
就像那样:
function calculate() {
var myBox1 = document.getElementById('box1').value;
var myBox2 = document.getElementById('box2').value;
var myBox3 = document.getElementById('box3').value;
var result = document.getElementById('result');
var value = (document.getElementById('changeValue').checked)?35:25;
var myResult = myBox1 * myBox2 * myBox3 / value;
result.value = myResult;
}
答案 4 :(得分:0)
function calculate() {
var myBox1 = document.getElementById('box1').value;
var myBox2 = document.getElementById('box2').value;
var myBox3 = document.getElementById('box3').value;
var result = document.getElementById('result');
var myResult = myBox1 * myBox2 * myBox3 / 25;
result.value = myResult;
//set initial state
$('#changeValue').change(function() {
if($(this).is(":checked")) {
var myDivide = myBox1 * myBox2 * myBox3 / 35;
return result.value = myDivide;
}
});
}