【问题标题】:Multiply the values in the "Enabled TextBoxes" Using JavaScript in ASP.Net在 ASP.Net 中使用 JavaScript 将“启用的文本框”中的值相乘
【发布时间】:2011-05-25 07:12:29
【问题描述】:

我有五个TextBox,比如TextBox的ID如下:

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

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

【问题讨论】:

  • 文本框输出为什么,输入?还是文本区域?
  • 所有文本框都带有整数值。
  • 请注意我对您选择的答案的评论——即来自@Srinivasan__

标签: javascript c# asp.net controls


【解决方案1】:
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;
}

【讨论】:

  • 注意:这个函数的参数应该是一个元素。不是元素的值。因此,您可以将 ClientID 传递给此函数
  • 如果所有文本框的值都是1,你会得到一个0
【解决方案2】:

你会使用 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

【讨论】:

    【解决方案3】:

    可以使用 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>
    

    【讨论】:

    • 您好 akhtar,我只想将启用的 TextBox 值与 txtQuantiy TextBox 值相乘。只有在从数据库中接收到文本框的值后,我们才知道启用了哪个文本框。
    • 假设您从 DB 中获取空值,您可以在计算任何文本框值之前添加检查。例如if(document.getElementById('').value != "" )
    • 是的。好的。但是我们如何在这里找到 txtD1 TextBox 已启用?假设如果 txtD2 & txtD3 被启用并且 txtD1 被禁用意味着,我们需要像这样乘以 txtTotal = txtQuantity * txtD2 * txtD3。
    • 你可以试试这个... if(document.getElementById('').disabled == "disabled")
    【解决方案4】:

    如果它们是 textarea,您可以使用它来查看它们是否已启用

    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);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 2011-12-19
      相关资源
      最近更新 更多