Javascript:输入值的双面转换
我正在尝试使用两个Javascript:输入值的双面转换,javascript,Javascript,我正在尝试使用两个标记来相互转换货币 HTML 如果我只在一侧键入一个数字,它会工作,但是如果我尝试在另一侧更改结果,整个脚本将完全失败,直到我刷新页面。尝试清除未使用的超时 var credits = document.getElementById('credits'), cash = document.getElementById('cash'); var cashId = null; var creditsId = null; function creditsToCash(){ i
标记来相互转换货币
HTML
如果我只在一侧键入一个数字,它会工作,但是如果我尝试在另一侧更改结果,整个脚本将完全失败,直到我刷新页面。尝试清除未使用的超时
var credits = document.getElementById('credits'),
cash = document.getElementById('cash');
var cashId = null;
var creditsId = null;
function creditsToCash(){
if (cashId != null) clearTimeout(cashId);
creditsId = setTimeout(function() {
crd = credits.value;
crd2ch = crd / 2.5;
cash.setAttribute('value', crd2ch);
}, 10);
}
function cashToCredits(){
if (creditsId != null) clearTimeout(creditsId);
cashId = setTimeout(function() {
ch = cash.value;
ch2crd = ch * 2.5;
credits.setAttribute('value', ch2crd);
}, 10);
}
当“整个脚本完全失败”时,您是否在控制台中收到任何错误消息?它不会给出任何错误。如果我试图更改转换后的值,脚本将停止处理两个输入标记。同样的问题。如果我更改了相反输入上的转换值,脚本将停止工作。
var credits = document.getElementById('credits'),
cash = document.getElementById('cash');
function creditsToCash(){
setTimeout(function() {
crd = credits.value;
crd2ch = crd / 2.5;
cash.setAttribute('value', crd2ch);
}, 10);
}
function cashToCredits(){
setTimeout(function() {
ch = cash.value;
ch2crd = ch * 2.5;
credits.setAttribute('value', ch2crd);
}, 10);
}
var credits = document.getElementById('credits'),
cash = document.getElementById('cash');
var cashId = null;
var creditsId = null;
function creditsToCash(){
if (cashId != null) clearTimeout(cashId);
creditsId = setTimeout(function() {
crd = credits.value;
crd2ch = crd / 2.5;
cash.setAttribute('value', crd2ch);
}, 10);
}
function cashToCredits(){
if (creditsId != null) clearTimeout(creditsId);
cashId = setTimeout(function() {
ch = cash.value;
ch2crd = ch * 2.5;
credits.setAttribute('value', ch2crd);
}, 10);
}