繁体   English   中英

如何在没有ajax的情况下将javascript动态数据发送到php变量

[英]how to send javascript dynamic data to php variable without ajax

我有一个带有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,我想刷新页面

您可以改为执行以下操作:

标记

<!-- 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/

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM