最近我做了一个叫做Converter的函数。它在早上工作得很好,现在当我使用它时,当我像3这样键入数字时,代码无法运行,它应该像三一样打印,但是我现在知道是什么问题,问题也没有显示。我是Java新手 这是代码
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function abc()
{
// alert('hello');
var amt=parseInt(document.getElementById('t1'));
var d="";
var ones=Array("","One","Two","Three","four","five");
var tens=Array("","","Twenty","Thirthy","Fourthy","fifthy");
var hundreds=Array("","One hundred","Two hundred","Three hundred","Four hundred","Five hundred");
if(amt>=1&&amt<=19)
{
d=ones[amt];
}
document.getElementById('p1').innerHTML=d;
// document.write(d);
}
</script>
</head>
<body>
<input type="text" id="t1" />
<input type="button" value="Convert" onclick="abc()" />
<p id="p1"></p>
</body>
</html>
答案 0 :(得分:0)
您忘记了解析文本框的值 应该是
document.getElementById('t1')。value
答案 1 :(得分:0)
除了我对不获取价值的评论。
使用let amt = document.getElementById('t1').value
。这将返回输入中的值。
有人为您在此处尝试做的事做了一个很好的例子:Convert digits into words with JavaScript
var a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
var b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
function inWords(num) {
if ((num = num.toString()).length > 9) return 'overflow';
n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
if (!n) return;
var str = '';
str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';
return str;
}
document.getElementById('number').onkeyup = function() {
document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);
};
<span id="words"></span>
<input id="number" type="text" />
答案 2 :(得分:-1)
您的错误是由以下行引起的:parseInt(document.getElementById('t1'));
您正在尝试parseInt(html输入元素)。无法将HTML值解析为int,因此它返回的是NaN,该NaN无法转换为“三”或任何其他数字。
这是工作代码:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function abc()
{
var amt=document.getElementById('t1').value;
var d="";
var ones=Array("","One","Two","Three","four","five");
var tens=Array("","","Twenty","Thirthy","Fourthy","fifthy");
var hundreds=Array("","One hundred","Two hundred","Three hundred","Four hundred","Five hundred");
if(amt>=1&&amt<=19)
{
d=ones[amt];
}
document.getElementById('p1').innerHTML=d;
// document.write(d);
}
</script>
</head>
<body>
<input type="text" id="t1" />
<input type="button" value="Convert" onclick="abc()" />
<p id="p1"></p>
</body>
</html>