动态var用于javascript中的乘法 [英] dynamic var for multiplication in javascript

查看:119
本文介绍了动态var用于javascript中的乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有asp.net网站,



哪里有文字框设备,人员和车辆的数量,价格和总数。

其中我乘以数量*价格=总价格。



我写了javascript n使用'onkeyup ='来调用这个js函数。



但遗憾的是它不起作用。



javascript

Hi Guys,

I have asp.net website,

Where i have textboxes for Qty, Price and totalpirce each for Equipment, Person and Vehical.
In which i'm multiplying Qty * Price = totalprice.

For that i wrote javascript n used 'onkeyup=""' to call this js function.

But unfortunately its not working.

javascript

<script type="text/javascript">
        function GetTotalPrice(txtqty, txtprice, txtTotalPrice) {
            alert(txtqty + txtprice);
            var qty = txtqty.value;
            var price = txtprice.value;            
            var TotalPrice = (qty * price);
            txtTotalPrice.value = TotalPrice;
        }
    </script>





asp.net



asp.net

<td>
<asp:Label ID="lbleprice" runat="server" Text="4"></asp:Label>
</td>
<td>
<asp:TextBox ID="txteqty" runat="server" onkeyup="GetTotalPrice(this, lbleprice, lbletprice)"></asp:TextBox>
</td>
<td>
<asp:Label ID="lbletprice" runat="server" ></asp:Label>
</td>





i将这三个字段放在Eq,person n vehical。



这就是为什么我想以这种方式调用那个函数...



任何人都可以检查我哪里错了。



谢谢



i have these three fields in Eq, person n vehical.

so that's why i want to call that function in that way...

Can any one plzzz check where i'm wrong.

Thanks

推荐答案

<table><tr>
<td>
    <asp:Label ID="lbleprice" runat="server" Text="4"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="txteqty" runat="server" onkeyup="GetTotalPrice(this, document.getElementById('lbleprice'), document.getElementById('lbletprice'))"></asp:TextBox>
    </td>
    <td>
    <asp:Label ID="lbletprice" runat="server" ></asp:Label>
    </td>
    </tr>
</table>
<script type="text/javascript">
    function GetTotalPrice(txtqty, txtprice, txtTotalPrice) {
        var qty = txtqty.value;
        var price = txtprice.innerHTML;
        var TotalPrice = (qty * price);
        if (isNaN(TotalPrice))
        {
            TotalPrice = '';
        }
        txtTotalPrice.innerHTML = TotalPrice;
    }


</script>


对不起,您对如何使用/处理事件的整个想法是错误的。实际 GetTotalPrice 调用的参数来自任何地方,它们在您的调用上下文中不存在。我甚至不想在你这样调用时会如何处理。你可以在调试器下看到它,但是没有太大的时间需要浪费时间,因为它总会失败。



第一个参数可以真正传递给事件处理程序是事件对象,它具有对事件目标的引用。在你的情况下,你不需要它,但这肯定不是数值而不是DOM对象:

https://developer.mozilla.org/en-US/docs/Web/Events/click [ ^ ],

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent [ ^ ]。



您甚至不需要它,因此您可以编写不带参数的事件处理程序。至于参数 txtqty txtprice txtTotalPrice ,你将不得不通过外部上下文传递它。但是你需要为你的事件处理程序创建一个特定的上下文,所以你宁愿需要在代码中设置事件处理程序,而不是在HTML元素属性中:

Sorry, your whole idea on how to use/handle events is wrong. The parameters to the actual GetTotalPrice call comes from nowhere, they don't exist in the context of your call. I don't even want to think how you handle will execute when called like that. You can see it under the debugger, but there is no a big need to waste time on that as it will always fail.

First parameter which can really be passed to event handler is the event object, which has a reference to the event target. In your case, you don't need it, but this is certainly not the numeric value and not a DOM object:
https://developer.mozilla.org/en-US/docs/Web/Events/click[^],
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent[^].

You don't even need it, so you can write an event handler without arguments. As to the arguments txtqty, txtprice, txtTotalPrice, you will have to pass it via the outer context. But you need to create a certain context for your event handler, so you would rather need to set up the event handler in code, not in the HTML element attribute:
var txteqty = document.getElementById("txteqty");

// ...

txteqty.onclick = function() { // or onkeyup, but it looks a bit weird to me
    var txtqty = // calculate if somehow, based on your code in the 
                 // outside context
    var txtprice = //... same thing
    var txtTotalPrice = //... same thing
    GetTotalPrice(txtqty, txtprice, txtTotalPrice); 
    // better call is "ShowTotalPrice", because you are not returning a value
    // ...
}





例如,如果您的三个参数来自其他元素,则可以使用 document.getElementById(properId)来计算字符串值。值然后将其解析为数字对象,例如: http://www.w3schools.com/ jsref / jsref_parseint.asp [ ^ ]。



-SA



If, for example, your three arguments come from other elements, you can calculate the string values using document.getElementById("appropriateId").value and then parse it to the number object, for example: http://www.w3schools.com/jsref/jsref_parseint.asp[^].

—SA


这篇关于动态var用于javascript中的乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆