如果使用输入值 id,则无法获得输出

Cannot get output if using input value id

它是一个量词转换。如果我使用 div 或 p 标签并使用它们的 id,那么它会显示输出。但是当我使用 input 和它的 id 时,什么都不显示。

正在尝试使用更改选项将金额转换为单词。它的印度货币转换。如果我使用输入选项,它会显示注意事项。如果这是一个愚蠢的问题,请原谅我,因为我不是专家。

$(document).ready(function () {
  var amount = document.getElementById('value').value;
  var fraction = Math.round(frac(amount) * 100);
  var f_text = "";
  if (fraction > 0) {
    f_text = "AND " + convert_number(fraction) + " Paisa";
  }
  var words = convert_number(amount) + " Rupee " + f_text + " Only";
  document.getElementById("word").innerHTML = words;
  return convert_number(amount) + " Rupee " + f_text + " Only";
});
function frac(f) {
  return f % 1;
}
function convert_number(number) {
  console.log('number: ' + number);
  if ((number < 0) || (number > 999999999)) {
    return "NUMBER OUT OF RANGE!";
  }
  var Gn = Math.floor(number / 10000000); 
  number -= Gn * 10000000; 
  var kn = Math.floor(number / 100000); 
  number -= kn * 100000; 
  var Hn = Math.floor(number / 1000); 
  number -= Hn * 1000; 
  var Dn = Math.floor(number / 100); 
  number = number % 100; 
  var tn = Math.floor(number / 10); 
  var one = Math.floor(number % 10); 
  var res = "";

  if (Gn > 0) {
    res += (convert_number(Gn) + " Crore");
  }
  if (kn > 0) {
    res += (((res == "") ? "" : " ") +
    convert_number(kn) + " Lakh");
  }
  if (Hn > 0) {
    res += (((res == "") ? "" : " ") +
    convert_number(Hn) + " Thousand");
  }
  if (Dn) {
    res += (((res == "") ? "" : " ") +
    convert_number(Dn) + " Hundred");
  }
    
    var ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
    
    var tens = Array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");
    
    if (tn > 0 || one > 0) {
      if (!(res == "")) {
        res += " ";
      }
      if (tn < 2) {
        res += ones[tn * 10 + one];
      }
      else {
        res += tens[tn];
        if (one > 0) {
          res += (" " + ones[one]);
        }
      }
    }
    if (res == "") {
      res = "Zero";
    }
    return res;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="value" />
<div id="word"></div>

它适用于 DIV 或 P 标签,因为脚本会在页面准备就绪时运行。

如果你希望它在更改后更新,你可以这样做 this:

$(document).ready(function () {
  $("#value").on("change", function() {
    var amount = $(this).val();
    
  var fraction = Math.round(frac(amount) * 100);
  var f_text = "";
  if (fraction > 0) {
    f_text = "AND " + convert_number(fraction) + " Paisa";
  }
  var words = convert_number(amount) + " Rupee " + f_text + " Only";
  document.getElementById("word").innerHTML = words;
  });
});

function frac(f) {
  return f % 1;
}
function convert_number(number) {
  console.log('number: ' + number);
  if ((number < 0) || (number > 999999999)) {
    return "NUMBER OUT OF RANGE!";
  }
  var Gn = Math.floor(number / 10000000); 
  number -= Gn * 10000000; 
  var kn = Math.floor(number / 100000); 
  number -= kn * 100000; 
  var Hn = Math.floor(number / 1000); 
  number -= Hn * 1000; 
  var Dn = Math.floor(number / 100); 
  number = number % 100; 
  var tn = Math.floor(number / 10); 
  var one = Math.floor(number % 10); 
  var res = "";

  if (Gn > 0) {
    res += (convert_number(Gn) + " Crore");
  }
  if (kn > 0) {
    res += (((res == "") ? "" : " ") +
    convert_number(kn) + " Lakh");
  }
  if (Hn > 0) {
    res += (((res == "") ? "" : " ") +
    convert_number(Hn) + " Thousand");
  }
  if (Dn) {
    res += (((res == "") ? "" : " ") +
    convert_number(Dn) + " Hundred");
  }
    
    var ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
    
    var tens = Array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");
    
    if (tn > 0 || one > 0) {
      if (!(res == "")) {
        res += " ";
      }
      if (tn < 2) {
        res += ones[tn * 10 + one];
      }
      else {
        res += tens[tn];
        if (one > 0) {
          res += (" " + ones[one]);
        }
      }
    }
    if (res == "") {
      res = "Zero";
    }
    return res;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="value" />
<div id="word"></div>