Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:输入值的双面转换_Javascript - Fatal编程技术网

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);
}