简体   繁体   English

使用文本框计算数字

[英]Calculating a number using a textbox

I've been trying to calculate a number using a number given by a user in a text box. 我一直在尝试使用用户在文本框中给出的数字来计算数字。 I've been trying to use the following code. 我一直在尝试使用以下代码。 But when I try to test it, nothing happens. 但是当我尝试测试时,没有任何反应。 Is there something I'm missing? 有什么我想念的吗? And is there a way that I can make the imprint variable global? 有没有办法让印记变量全局化?

     <form>
        <p>How many products do you want
            ingraved?<input id="imprint_amount" name="imprint_amount" type="text"/>
        </p>
        <p>Names to be Imprinted(one per
            line)<TEXTAREA COLS=25 NAME="imprint_text" ROWS=5 WRAP=HARD style="resize:none;"></textarea>
        </p>
         <input onclick="imprint_price" type="button" value="Finish"/>
        <p id="total_cost"></p>
    </form>
    <script type="text/javascript">

        function imprint_price() {
            var imprint_cost,
                imprint_quality,
                imprint_total;
            imprint_cost = 10.99;
            imprint_quantity = document.getElementById('imprint_amount');
            imprint_total = $imprint_cost * parseInt(imprint_quantity, 10);
            document.getElementById('total_cost') = "$" + imprint_total;

        }

Thanks, Traci 谢谢,Traci

You will want to use the value property of that input element you are referencing in your variable: 您将需要使用您在变量中引用的输入元素value属性:

… parseInt(imprint_quantity.value, 10);

For arbitrary HTML elements, you need to use textContent (or innerText to support old IE): 对于任意HTML元素,您需要使用textContent (或innerText来支持旧IE):

document.getElementById('total_cost').textContent = …;

Assigning to an expression as you did should have thrown a quite accurate exception, check your browser's error console for them. 像你一样分配给表达式应该抛出一个非常准确的异常,检查浏览器的错误控制台。

In this line, you'll want to set the innerHTML of the element. 在这一行中,您需要设置元素的innerHTML

document.getElementById('total_cost').innerHTML = "$" + imprint_total;

This basically sets the text inside the <p></p> to be <p>$x.xx</p> . 这基本上将<p></p>内的文本设置为<p>$x.xx</p>

And also this line should be 这条线也应该是

imprint_quantity = document.getElementById('imprint_amount').value;

which retrieves the value from the textbox. 它从文本框中检索值。

Furthermore, when defining the variables, you wrote "quality". 此外,在定义变量时,您写了“质量”。 It should be 它应该是

imprint_quantity,

Change your javascript to: 将您的javascript更改为:

<script type="text/javascript">

    function imprint_price() {
        var imprint_cost,
            imprint_quantity,
            imprint_total;
        imprint_cost = 10.99;
        imprint_quantity = document.getElementById('imprint_amount').value;
        imprint_total = imprint_cost * parseInt(imprint_quantity, 10);
        document.getElementById('total_cost').innerHTML = imprint_total;
    }
</script>

Working jsFiddle here http://jsfiddle.net/Zt38S/2/ 在这里工作jsFiddle http://jsfiddle.net/Zt38S/2/

imprint_quantity = document.getElementById('imprint_amount'); imprint_quantity = document.getElementById('imprint_amount');

= =

imprint_quantity = document.getElementById('imprint_amount').value(); imprint_quantity = document.getElementById('imprint_amount')。value();

Lemme know if that fixes it, a common enough mistake. Lemme知道是否修复了它,这是一个常见的错误。

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

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