将“启用的文本框”中的值相乘在 ASP.Net 中使用 JavaScript

发布于 2024-11-09 10:48:20 字数 337 浏览 0 评论 0原文

我有五个文本框,例如文本框的 ID 如下:

     1. txtD1
     2. txtD2
     3. txtD3
     4. txtQuantity
     5. txtTotal

这里,文本框 txtD1、txtD2 和 txtD3 根据数据库中的值启用或禁用。在前端,我们不知道哪些文本框将被启用或禁用。在 txtQuantity 中我想输入数字。现在我想将启用的 TextBox 值与 txtQuantity 相乘,并且想将该值显示到 txtTotal TextBox 中。如何做到这一点?同时我想将文本框作为函数的参数传递。因此,无论我们想要在哪里,它都会很有用。

I have five TextBoxes such as ID of the TextBoxes are as follows:

     1. txtD1
     2. txtD2
     3. txtD3
     4. txtQuantity
     5. txtTotal

Here, the TextBoxes txtD1, txtD2 and txtD3 are Enabled Or Disabled based on the values from the DataBase. In the front end we dont know which are the TextBoxes will be enabled or disabled. In txtQuantity I want to enter the Number. Now I want to multiply the enabled TextBox values with the txtQuantity and I want to Display the value into the txtTotal TextBox. How to do this? At the same time I want to pass the textboxes as function's argument. So it will be useful, wherever we wants.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

俏︾媚 2024-11-16 10:48:20
function MultiplyValues(txtBox1,txtBox2,txtBox3,txtQuantity)
{
    var ReturnValue = 1;
    if (txtBox1.disabled==false && txtBox1.value.lenth>0)
        ReturnValue *= txtBox1.value;
    if (txtBox2.disabled==false && txtBox2.value.lenth>0)
        ReturnValue *= txtBox2.value;
    if (txtBox3.disabled==false && txtBox3.value.lenth>0)
        ReturnValue *= txtBox3.value;
    if (txtQuantity.value.length>0)
        ReturnValue *= txtQuantity.value;
    document.getElementById("txtTotal").value = (ReturnValue==1)?0:ReturnValue;
}

上述函数将能够根据文本框状态(启用或禁用)和 txtTotal 中的显示来计算值。您可以将此函数应用于按钮onclick。活动

@alex,感谢您告知,我错过了这个案例。 PFB,修改功能

function MultiplyValues(txtBox1,txtBox2,txtBox3,txtQuantity)
{
    var ReturnValue = 1;
    var isThere = 0;
    if (txtBox1.disabled==false && txtBox1.value.length>0) {
        ReturnValue *= txtBox1.value; isThere=1; }
    if (txtBox2.disabled==false && txtBox2.value.length>0) {
        ReturnValue *= txtBox2.value; isThere=1; }
    if (txtBox3.disabled==false && txtBox3.value.length>0) {
        ReturnValue *= txtBox3.value; isThere=1; }
    if (txtQuantity.value.length>0) {
        ReturnValue *= txtQuantity.value; isThere=1; }
    document.getElementById("txtTotal").value = (ReturnValue==1 && isThere==0)?0:ReturnValue;
}
function MultiplyValues(txtBox1,txtBox2,txtBox3,txtQuantity)
{
    var ReturnValue = 1;
    if (txtBox1.disabled==false && txtBox1.value.lenth>0)
        ReturnValue *= txtBox1.value;
    if (txtBox2.disabled==false && txtBox2.value.lenth>0)
        ReturnValue *= txtBox2.value;
    if (txtBox3.disabled==false && txtBox3.value.lenth>0)
        ReturnValue *= txtBox3.value;
    if (txtQuantity.value.length>0)
        ReturnValue *= txtQuantity.value;
    document.getElementById("txtTotal").value = (ReturnValue==1)?0:ReturnValue;
}

The above function will able to calculate the value based up on the textbox status(enabled or disabled) and the display in the txtTotal. You can apply this function to a button onclick. event

@alex, Thanks for informing, I missed this case. PFB, Modified function

function MultiplyValues(txtBox1,txtBox2,txtBox3,txtQuantity)
{
    var ReturnValue = 1;
    var isThere = 0;
    if (txtBox1.disabled==false && txtBox1.value.length>0) {
        ReturnValue *= txtBox1.value; isThere=1; }
    if (txtBox2.disabled==false && txtBox2.value.length>0) {
        ReturnValue *= txtBox2.value; isThere=1; }
    if (txtBox3.disabled==false && txtBox3.value.length>0) {
        ReturnValue *= txtBox3.value; isThere=1; }
    if (txtQuantity.value.length>0) {
        ReturnValue *= txtQuantity.value; isThere=1; }
    document.getElementById("txtTotal").value = (ReturnValue==1 && isThere==0)?0:ReturnValue;
}
橙幽之幻 2024-11-16 10:48:20

你能使用 jQuery 吗?如果可以的话,使用 jQuery 检查哪些已启用,然后将它们全部加起来。我在 http://jsfiddle.net/2SfX3/ 有一个工作示例

如果你有这样的html:

<input type="text" class="factor" id="txtD1"/>
<input type="text" class="factor" id="txtD2" disabled="disabled"/>
<input type="text" class="factor" id="txtD3" disabled="disabled"/>
<input type="text" id="txtQuantity" />
<input type="text" id="txtTotal" />
<input type="button" id="compute" value="Compute" />

你可以实现您的 jQuery 如下:

$(document).ready(function() {
    $('#compute').click(function() {
        var total = 0;
        $.each($('.factor'), function(i, data) {
            if (!$(this).attr('disabled')) {
                total = total + parseInt($(this).val());
            }
        });   
 $('#txtTotal').val(parseInt($('#txtQuantity').val()) * total);
    });
});

此输出将是第一个文本框值 * 数量,因为 txtD1 是唯一启用的。

  • 这是假设您正在处理整数。否则您可以使用 parseFloat 。

Can you use jQuery? If you can, use jQuery to check which ones are enabled then sum them all. I have a working sample at http://jsfiddle.net/2SfX3/

If you have html like:

<input type="text" class="factor" id="txtD1"/>
<input type="text" class="factor" id="txtD2" disabled="disabled"/>
<input type="text" class="factor" id="txtD3" disabled="disabled"/>
<input type="text" id="txtQuantity" />
<input type="text" id="txtTotal" />
<input type="button" id="compute" value="Compute" />

You can implement your jQuery as this:

$(document).ready(function() {
    $('#compute').click(function() {
        var total = 0;
        $.each($('.factor'), function(i, data) {
            if (!$(this).attr('disabled')) {
                total = total + parseInt($(this).val());
            }
        });   
 $('#txtTotal').val(parseInt($('#txtQuantity').val()) * total);
    });
});

The output of this will be the first textbox value * quantity, because txtD1 is the only one enabled.

  • This is assuming you are dealing with integers. You can use parseFloat otherwise.
情场扛把子 2024-11-16 10:48:20

您可以使用JavaScript的onblur事件并调用JS方法来计算总计并将其分配给Total Textbox:

<asp:TextBox ID="txtQuantity" runat="server" onblur="javascript:update();"></asp:TextBox>

<script language="javascript" type="text/javascript">
     function update() {
         document.getElementById('<%= txtTotal.ClientID %>').value = 
         document.getElementById('<%= txtQuantity.ClientID %>').value *
         document.getElementById('<%= txtD1.ClientID %>').value;
     }

</script>

You can use the onblur event of JavaScript and call the JS method to calculate and assign the total to the Total Textbox:

<asp:TextBox ID="txtQuantity" runat="server" onblur="javascript:update();"></asp:TextBox>

<script language="javascript" type="text/javascript">
     function update() {
         document.getElementById('<%= txtTotal.ClientID %>').value = 
         document.getElementById('<%= txtQuantity.ClientID %>').value *
         document.getElementById('<%= txtD1.ClientID %>').value;
     }

</script>
哆啦不做梦 2024-11-16 10:48:20

如果它们是文本区域,您可以使用它来查看它们是否已启用

var tags = document.getElementsByTagName('textarea');

var length = tags.length;

for (var i;i <length; i++){
   if (tags[i].disabled == false) {
     // now you can do something with this tag this enabled
     // example
     alert(tags[i].value);
   }
}

in case they are textarea you can use this to see if they are enabled

var tags = document.getElementsByTagName('textarea');

var length = tags.length;

for (var i;i <length; i++){
   if (tags[i].disabled == false) {
     // now you can do something with this tag this enabled
     // example
     alert(tags[i].value);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文