Javascript 需要简单计算器的帮助吗 函数fadd() { var first,sec,res; first=parsefloat(document.forms[0].txt1st.value); sec=parsefloat(document.forms[0].txt2nd.value); res=第一次+秒; document.forms[0].txtres.value=res; } 输入第一个数字 输入第二个数字 结果
编辑:此答案在问题更新之前发布 不能对以数字开头的属性名称使用点表示法 不要使用Javascript 需要简单计算器的帮助吗 函数fadd() { var first,sec,res; first=parsefloat(document.forms[0].txt1st.value); sec=parsefloat(document.forms[0].txt2nd.value); res=第一次+秒; document.forms[0].txtres.value=res; } 输入第一个数字 输入第二个数字 结果,javascript,Javascript,编辑:此答案在问题更新之前发布 不能对以数字开头的属性名称使用点表示法 不要使用文档.forms[0].1st。使用document.getElementById('1st')。这同样适用于2nd <html> <head> <script language="javascript"> function fadd() { var first,sec,res; first=parsefloat(document.forms[0].txt1st.value); s
文档.forms[0].1st
。使用document.getElementById('1st')
。这同样适用于2nd
<html>
<head>
<script language="javascript">
function fadd()
{
var first,sec,res;
first=parsefloat(document.forms[0].txt1st.value);
sec=parsefloat(document.forms[0].txt2nd.value);
res=first+sec;
document.forms[0].txtres.value=res;
}
</script>
</head>
<body>
<form>
Enter 1st number <input name="txt1st" id="txt1st" type="text">
</br>
Enter 2nd number <input name="txt2nd" id="txt2nd" type="text">
</br>
Result
<input name="txtres" id="txtres" type="text">
</br>
<input name="btnadd" id="btnadd" type="button" value="+" onclick="fadd()">
<input name="btnsub" id="btnsub" type="button" value="-" onclick="fminus()">
<input name="btndiv" id="btndiv" type="button" value="%" onclick="fdiv()">
<input name="btnmul" id="btnmul" type="button" value="*" onclick="fmult()">
</form>
</body>
</html>
编辑:此答案在问题更新之前发布 不能对以数字开头的属性名称使用点表示法 不要使用
文档.forms[0].1st
。使用document.getElementById('1st')
。这同样适用于2nd
<html>
<head>
<script language="javascript">
function fadd()
{
var first,sec,res;
first=parsefloat(document.forms[0].txt1st.value);
sec=parsefloat(document.forms[0].txt2nd.value);
res=first+sec;
document.forms[0].txtres.value=res;
}
</script>
</head>
<body>
<form>
Enter 1st number <input name="txt1st" id="txt1st" type="text">
</br>
Enter 2nd number <input name="txt2nd" id="txt2nd" type="text">
</br>
Result
<input name="txtres" id="txtres" type="text">
</br>
<input name="btnadd" id="btnadd" type="button" value="+" onclick="fadd()">
<input name="btnsub" id="btnsub" type="button" value="-" onclick="fminus()">
<input name="btndiv" id="btndiv" type="button" value="%" onclick="fdiv()">
<input name="btnmul" id="btnmul" type="button" value="*" onclick="fmult()">
</form>
</body>
</html>
两件事:
parsefloat
应该是parsefloat
。函数名区分大小写
1st
不是合法的ID(我也不认为它是合法的名称)。此外,您不能在点符号(x.y.z
)中引用非标识符(1st
不是标识符,因为它以数字开头)。您可以尝试document.forms[0]['1st'].value
,也可以尝试将1st
重命名为first
(将2nd
重命名为second
)。两件事:
parsefloat
应该是parsefloat
。函数名区分大小写
1st
不是合法的ID(我也不认为它是合法的名称)。此外,您不能在点符号(x.y.z
)中引用非标识符(1st
不是标识符,因为它以数字开头)。您可以尝试document.forms[0]['1st'].value
,也可以尝试将1st
重命名为first
(将2nd
重命名为second
)。此外,您应该更改此行:
var x = {};
x['1st'] = 10;
console.log(x['1st']); // This works
console.log(x.1st); // This doesn't - Syntax error
为此:
document.forms[0].textres.value=res;
在听了这里其他帖子的建议并这样做之后,您的代码应该可以正常工作。此外,您应该更改这一行:
var x = {};
x['1st'] = 10;
console.log(x['1st']); // This works
console.log(x.1st); // This doesn't - Syntax error
为此:
document.forms[0].textres.value=res;
在听了其他帖子的建议并这么做之后,您的代码应该可以正常工作。@Gaby:是的,很好。。。此外,还有一个
parseFloat
问题@Gaby:是的,很好。。。此外,还有parseFloat
问题