【发布时间】:2018-07-21 08:39:42
【问题描述】:
我有一组带有 ID 的 HTML 数字输入
YXS, YSM, YMED
我可以获取每个输入的值,将它们相加,然后将结果显示到#output。
但是,它仅在按 YXS、YSM、YMED 顺序输入时有效
如果我删除 YSM 使其空白,我只会将 YXS 作为值输出到#output。
我正在尝试使用它,所以无论输入什么值,它们要么加在一起并显示,或者如果只输入一个值,那么无论输入顺序如何,该值都会显示到#output。
我整天都在摸不着头脑,所以尽可能寻求帮助!
这是我的代码:
$('#YXS').on("input", function() {
var yxsInput = this.value;
console.log(yxsInput);
var Silver = (yxsInput / 100) * 90;
var Gold = (yxsInput / 100) * 85;
var Plat = (yxsInput / 100) * 80;
var totalPrice = Number(yxsInput);
$('#output').text(yxsInput);
$("#output_Silver").html(Silver.toFixed(2));
$("#output_Gold").html(Gold.toFixed(2));
$("#output_Plat").html(Plat.toFixed(2));
$('#YSM').on("input", function() {
var ysmInput = this.value;
console.log(ysmInput);
var totalPrice = Number(yxsInput)+Number(ysmInput);
var Silver = (totalPrice / 100) * 90;
var Gold = (totalPrice / 100) * 85;
var Plat = (totalPrice / 100) * 80;
$('#output').text(totalPrice);
$("#output_Silver").html(Silver.toFixed(2));
$("#output_Gold").html(Gold.toFixed(2));
$("#output_Plat").html(Plat.toFixed(2));
$('#YMED').on("input", function() {
var ymedInput = this.value;
console.log(ymedInput);
var totalPrice = Number(yxsInput)+Number(ysmInput)+Number(ymedInput);
var Silver = (totalPrice / 100) * 90;
var Gold = (totalPrice / 100) * 85;
var Plat = (totalPrice / 100) * 80;
$('#output').text(totalPrice);
$("#output_Silver").html(Silver.toFixed(2));
$("#output_Gold").html(Gold.toFixed(2));
$("#output_Plat").html(Plat.toFixed(2));
});
});
我尝试过先获取输入
$('#YXS').on("input", function() {
var yxsInput = this.value;
console.log(yxsInput);
$('#YSM').on("input", function() {
var ysmInput = this.value;
console.log(ysmInput);
$('#YMED').on("input", function() {
var ymedInput = this.value;
console.log(ymedInput);
这也没有解决,控制台会告诉我 yxsInput(例如)没有定义。
希望我已经充分解释了我的最终目标是什么!
非常感谢,
【问题讨论】:
-
Java != JavaScript
标签: javascript php jquery html calculator