我有一个HTML页面,其中有两个Textbox和一个Button。我编写了一个Compute函数来在Textbox中显示结果,但它不起作用。警报不会在页面上触发。
任何人都可以帮助我,我哪里出错了。
<script type="text/javascript">
function isNumericKey(e)
{
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e)
{
var charCode = e.which;
}
else
{
return true;
}
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
function submitMyNumber()
{
alert(1);
var x = document.getElementById('myInput').value;
return x.match(/^[0-9]+$/) != null;
var y = document.getElementById('myInput1').value;
return y.match(/^[0-9]+$/) != null;
compute();
}
function compute()
{
alert(1);
var a,b,c;
var qtc=document.form1.myInput.value;
alert(qtc);
var hr=document.getByElementById('myinput1').value;
alert(hr);
a=60/hr;
alert(a);
b=math.sqrt(a);
alert(b);
c=qtc/b;
alert(c);
document.write(a + "" + b+ " " + c+ "")
}
</script>
</head>
<body>
<form>
<table align="center" style="border:2px solid black">
<tr>
<td>Qtc:</td>
<td><input type="text" id="myInput" name="myInput" onkeypress="return isNumericKey(event);" /><br /></td></tr>
<tr>
<td>Hr:</td>
<td><input type="text" id="myInput1" name="myInput1" onkeypress="return isNumericKey(event);" /><br />
</td>
</tr>
<tr>
<td> <input type="submit" id="mySubmit" name="mySubmit" value="Submit only Number" onclick="compute()" />
</td>
</tr>
<tr><td><input type="text" id="result" name="result" value="" /></td></tr>
</table>
</form>
</body>
</html>
答案 0 :(得分:2)
在这个函数中
function submitMyNumber() {
var x = document.getElementById('myInput').value;
return x.match(/^[0-9]+$/) != null; // <-- return
var y = document.getElementById('myInput1').value;
return y.match(/^[0-9]+$/) != null;
compute();
}
在函数返回的第二行,因此所有下一行都不会被执行。
如果我不得不猜你可能想做类似
的事情function submitMyNumber() {
var x = document.getElementById('myInput').value;
if (!x.match(/^[0-9]+$/)) { return false }
var y = document.getElementById('myInput1').value;
if (!y.match(/^[0-9]+$/)) { return false }
compute();
}
答案 1 :(得分:0)
代码中可能出现的错误
答案 2 :(得分:0)
您的代码中没有表单名称:
从<form>
到
<form name = "form1" >
PS:
当您使用document.form1.myInput.value;
您告诉从名称为myInput
form1
值
答案 3 :(得分:0)
将return语句更改为if语句:
function submitMyNumber()
{
alert(1);
var x = document.getElementById('myInput').value;
if (x.match(/^[0-9]+$/) === null ) {
return;
}
var y = document.getElementById('myInput1').value;
if (y.match(/^[0-9]+$/) === null ) {
return;
}
compute();
}
答案 4 :(得分:0)
我认为功能应该是:
function submitMyNumber() {
var x = document.getElementById('myInput').value;
var x_match= x.match(/^[0-9]+$/) != null;
//Store x_match in hiddenfield
var y = document.getElementById('myInput1').value;
var y_match= y.match(/^[0-9]+$/) != null;
//Store y_match in hiddenfield
compute();
return;
}
答案 5 :(得分:0)
您的代码中存在拼写错误: 更新:Fiddle
在compute()
函数
getByElementById('myinput1')
应为getElementById('myInput1')
math.sqrt(a);
应为Math.sqrt(a);
该功能应该看起来像
function compute(){
var a,b,c;
var qtc=document.form1.myInput.value;
alert(qtc);
var hr=document.getElementById('myInput1').value;
alert(hr);
a=60/hr;
alert(a);
b=Math.sqrt(a);
alert(b);
c=qtc/b;
alert(c);
document.write(a + "" + b+ " " + c+ "")
//var result = document.getElementById('result');
//result.value = c;
}
您的html form
必须有一个名称= form1 <form name="form1">
<form name="form1">
<table align="center" style="border:2px solid black">
<tr>
<td>Qtc:</td>
<td><input type="text" id="myInput" name="myInput" onkeypress="return isNumericKey(event);" /><br /></td>
</tr>
<tr>
<td>Hr:</td>
<td><input type="text" id="myInput1" name="myInput1" onkeypress="return isNumericKey(event);" /><br /></td>
</tr>
<tr>
<td><button type="button" onclick="compute(); return false;">Submit only Number</button></td>
</tr>
<tr><td><input type="text" id="result" name="result" value="" /></td></tr>
</table>
</form>
答案 6 :(得分:0)
上面的代码中有很多javascript错误我不知道你要做什么,但正确的javascript将是
function compute()
{
alert(1);
var a,b,c;
var qtc=document.form1.myInput.value;
alert(qtc);
var hr=document.getElementById('myInput1').value;
alert(hr);
a=60/hr;
alert(a);
b=Math.sqrt(a);
alert(b);
c=qtc/b;
alert(c);
document.getElementById('result').value = c;
//document.write(a + "" + b+ " " + c+ "")
return false;
}
加上表格标签没有名称字段。应该是这样的
<form onsubmit="return false;" name="form1">
并且提交按钮应该返回false
像
<input type="submit" id="mySubmit" name="mySubmit" value="Submit only Number" onclick="compute(); return false;" />或者表单将继续提交是否有javascript错误
你的工具像firebug一样找到你的javascript错误