简体   繁体   English

使用javascript问题填充输入字段

[英]populate input field using javascript problem

Thanks for the responses.. i put a live version of the code.. to see if that helps resolve this. 感谢您的答复..我放置了代码的实时版本..看是否有助于解决此问题。 It is here: 是这里:

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

I updated the following code to reflect what was offered as an answer.. however this just returns zero 我更新了以下代码以反映提供的答案..但是,这仅返回零

I am having trouble wrapping my head around creating a simple equation in javaScript. 我很难在javaScript中创建一个简单的方程式。 What I am trying to achieve is to have a=(x/z)*y . 我正在尝试实现的是a=(x/z)*y

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

What I came up with is 我想出的是

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

which I am not sure is even close. 我不确定那是哪一个。 Can someone help guide me through this, please? 有人可以帮我解决这个问题吗?

I see a couple of things that are not working in your script: 我发现您的脚本中有几项不起作用:

  • The year variable is not used and there is no year input in your form. 不使用year变量,并且您的表格中没有year输入。

  • The a , b and c variable declaration, I think that you want to get the values from your input elements. 我认为abc变量声明要从输入元素中获取值。

  • You assign the calculation to the productTotal variable, and later you use the undefined theProductTotal variable. 您分配计算到productTotal变量,以后你使用未定义的theProductTotal变量。

To get the form elements by name I would recommend you something like this: 要按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;
}

Notice how I'm getting the input values, since you are using only the name attribute, you can access the elements in that way. 请注意,我是如何获取输入值的,因为仅使用name属性,因此可以通过这种方式访问​​元素。

Also look that I'm converting the input values to Number (by using the unary plus operator), since the value property is string, and that can give you problems when you sum, because if you use the add operator with strings a concatenation will take place. 还应注意,由于value属性是字符串,因此我正在将输入值转换为Number(通过使用一元加号运算符),这在求和时会给您带来问题,因为如果将add运算符与字符串一起使用,则会串联发生。

Check the above example here . 此处检查以上示例。

Using JQuery a working code would be: 使用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>

Hopefully I haven't missed anything. 希望我什么都没错过。

Use .val() instead. 使用.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>

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

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