2009-10-28 121 views
0

感谢您的回复..我把代码的现场版本..看看是否有助于解决这个问题。正是在这里:使用javascript问题填充输入字段

http://www.zacharydesigns.com/sandbox/calculatorJS.html

我更新了下面的代码,以反映什么是提供一个答案。但是这只是返回零

我无法缠绕我的头周围创造一个简单的等式的JavaScript。我想实现的是有a=(x/z)*y

x = user input 
z = total of all input 
y = defined number (in this case 300) 

我想出了是

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 

<script type="text/javascript"> 
function update() 
{ 
    var a = $("#productOne").val(); // '#' identify IDs as in CSS 
    var b = $("#productTwo").val(); 
    var c = $("#productThree").val(); 
    var productTotal = Math.floor(a/(a+b+c)); 
    $("#productTotal").val(productTotal); // Set calculated value in the HTML 
} 

</script> 

<form name="calc" action="#"> 
    <table align="center" border="1"> 
     <tr> 
       <td align="right"> 
         Product One: 
       </td> 
       <td align="left"> 
         <input type="text" name="productOne" id="productOne" size="5" /></td> 
       <td align="right"> 
         Product Two: 
       </td> 
       <td align="left"> 
         <input type="text" name="productTwo" id="productTwo" size="5" /></td> 
       <td align="right"> 
         Product Three: 
       </td> 
       <td align="left"> 
         <input type="text" name="productThree" id="productThree" size="5" /></td> 
     </tr> 
     <tr> 
       <td colspan="2" align="center"> 
         <input type="button" value="Calculate" onclick="update();return false;" /> 
       </td> 
     </tr> 
     <td align="right"> 
       Product Total:</td> 
     <td align="left"> 
       <input type="text" name="productTotal" id="productTotal" size="6" readonly="readonly" /></td> 
     </tr></table> 
</form> 

这我不知道是差得远。有人可以帮助指导我完成这个吗?

+0

你的问题在哪里?与输入你想要的预期输出? – TStamper 2009-10-28 21:15:21

+0

就我所了解的代码而言,他只是想用其他输入的计算值更新HTML输入字段。 – 2009-10-28 21:19:17

+0

应用了一些缩进之后,显然有一个''标签丢失。 – Svante 2009-10-28 21:22:23

回答

3

我看到一对夫妇的未在你的脚本工作的事:

  • year变量不使用,有一个在您的形式没有year输入。

  • a,bc变量声明,我认为你想要从你的输入元素获取值。

  • 将计算分配给productTotal变量,稍后使用未定义的theProductTotal变量。

要获取name表单元素,我会建议你这样的事情:

function update() { 
    var a = +document.forms['calc'].elements['productOne'].value, 
     b = +document.forms['calc'].elements['productTwo'].value, 
     c = +document.forms['calc'].elements['productThree'].value, 
     productTotal = Math.floor(a/(a+b+c)*300); 

    document.forms['calc'].elements['productTotal'].value = productTotal; 

    return false; 
} 

通知我应得的输入值,因为你只使用name属性,可以以这种方式访问​​元素。

另外看看我将输入值转换为数字(通过使用一元加运算符),因为value属性是字符串,并且在总结时会给你带来问题,因为如果你使用add运算符串将会发生。

检查上述示例here

0

使用JQuery工作代码为:

<script type="text/javascript"> 

function update() { 

var a = parseInt($("#productOne")val()); // '#' identify IDs as in CSS 
var b = parseInt($("#productTwo").val()); 
var c = parseInt($("#productThree").val()); 
var productTotal = Math.floor(a/(a+b+c)*300); 


$("#productTotal").attr("value",productTotal); // Set calculated value in the HTML 

} 
</script> 

<form name="calc" action="#"> 

<table align="center" 
border="1"><tr><td align="right">Product One: </td> 
<td align="left"><input type="text" name="productOne" id="productOne" size="5" /> 
</td> 
<td align="right">Product Two: </td> 
<td align="left"><input type="text" name="productTwo" id="productTwo" size="5" /> 
</td> 
<td align="right">Product Three: </td> 
<td align="left"><input type="text" name="productThree" id="productThree" size="5" /> 
</td> 
</tr><tr><td colspan="2" align="center"> 
<input type="button" value="Calculate" 
onclick="update();return false;" /> </td></tr> 

<td align="right">Product Total:</td><td align="left"> 
<input type="text" name="productTotal" id="productTotal" size="6" 
readonly="readonly" /></td></tr> 
</table></form> 

希望我没有错过任何东西。

+1

你应该'a','b'和'c'变量转换为数字,否则'(a + b + c) '表达式将被评估为字符串连接。 – CMS 2009-10-28 21:34:24

+0

谢谢Gergely ...不工作只是返回零..我在这里举了一个生动的例子: http://www.zacharydesigns.com/sandbox/calculatorJS.html – Zac 2009-10-28 21:34:27

+0

更正将字符串解析为整数。感谢您的通知。 – 2009-10-28 21:57:20

0

改为使用.val()。

<script type="text/javascript"> 
function update() 
{ 
    var a = parseInt($("#productOne").val()); // '#' identify IDs as in CSS 
    var b = parseInt($("#productTwo").val()); 
    var c = parseInt($("#productThree").val()); 
    var productTotal = Math.floor(a/(a+b+c)*300); 
    $("#productTotal").val(productTotal); // Set calculated value in the HTML 
} 

</script> 

<form name="calc" action="#"> 
    <table align="center" border="1"> 
     <tr> 
      <td align="right"> 
       Product One: 
      </td> 
      <td align="left"> 
       <input type="text" name="productOne" id="productOne" size="5" /></td> 
      <td align="right"> 
       Product Two: 
      </td> 
      <td align="left"> 
       <input type="text" name="productTwo" id="productTwo" size="5" /></td> 
      <td align="right"> 
       Product Three: 
      </td> 
      <td align="left"> 
       <input type="text" name="productThree" id="productThree" size="5" /></td> 
     </tr> 
     <tr> 
      <td colspan="2" align="center"> 
       <input type="button" value="Calculate" onclick="update();return false;" /> 
      </td> 
     </tr> 
     <td align="right"> 
      Product Total:</td> 
     <td align="left"> 
      <input type="text" name="productTotal" id="productTotal" size="6" readonly="readonly" /></td> 
     </tr></table> 
</form> 
+0

谢谢埃里克... couldnt得到那工作要么..我更新了代码与你在这里: http://www.zacharydesigns.com/sandbox/calculatorJS.html – Zac 2009-10-28 21:36:27

+1

'val()'是比正确的更正确'attr()',但是这仍然是将字符串加在一起而不是整数。 – bobince 2009-10-28 21:47:15