我有一个表单,带有3个输入类型的文本。
<input type="text" id="one" onkeyup="multiply()" name="one">
<input type="text" id="two" name="two">
<input type="text" id="three" name="three">
<script>
function multiply(){
one = document.getElementById('one').value;
two = document.getElementById('two').value;
document.getElementById('three').value = one * two
}
</script>
现在我没有三个值,但它是一个动态的,当我提交论坛(forumSubmit.php)然后我得到错误
undefiend index three
我搜索过&amp;发现这可以用ajax完成,但我不想使用ajax,我想让页面刷新
答案 0 :(得分:1)
你可以这样做:
<强>标记强>
<!-- Use onkeyup on both inputs -->
<input type="text" id="one" onkeyup="multiply()" name="one">
<input type="text" id="two" onkeyup="multiply()" name="two">
<input type="text" id="three" name="three">
<强>的JavaScript 强>
function multiply() {
// Parse the values, and count it as 0 if the input is not a number
// I also made the variables private to this function using the var keyword
// There is no need to have them in the global namespace
var one = parseInt(document.getElementById('one').value, 10) || 0;
var two = parseInt(document.getElementById('two').value, 10) || 0;
document.getElementById('three').value= one * two;
}
工作示例
答案 1 :(得分:0)
<input type="text" id="one" onkeyup="multiply()" name="one" />
<input type="text" id="two" onkeyup="multiply()" name="two" />
<input type="text" id="three" name="three" />
<script type="text/javascript">
function multiply() {
one = document.getElementById('one').value;
two = document.getElementById('two').value;
document.getElementById('three').value = one * two;
}
</script>