2012-09-09 79 views
1

我有一個表單,3輸入類型的文本。如何發送JavaScript動態數據到php變量沒有AJAX

​​3210

現在我沒有價值的三個,但它是一個動態的,當我提交論壇(forumSubmit.php)然後我得到我搜索&的

undefiend index three 

發現錯誤這可以使用Ajax來完成,但我不希望使用AJAX,我想打一個頁面刷新

+0

如果設置的值 - 在發佈表單之前通過JavaScript將該元素的屬性傳遞,該值將隨着表單的提交而傳遞。 –

+1

請查看你的代碼,很多東西不能這樣工作 – dbf

+0

你是什麼意思通過設置javascript的動態值? –

回答

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

工作實例

放在一起演示:http://jsfiddle.net/DjQNx/