1

我有一个表格,带有 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

我搜索并发现这可以用 ajax 完成,但我不想使用 ajax,我想刷新页面

4

2 回答 2

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;
}​

工作示例

整理一个demo:http: //jsfiddle.net/DjQNx/

于 2012-09-09T13:17:09.607 回答
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>
于 2012-09-09T13:16:57.590 回答