簡體   English   中英

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

[英]Multiply the values in the “Enabled TextBoxes” Using JavaScript in ASP.Net

我有五個TextBoxes,例如TextBoxes的ID如下:

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

此處,文本框 txtD1、txtD2 和 txtD3 根據數據庫中的值啟用或禁用。 在前端,我們不知道哪些文本框將被啟用或禁用。 在 txtQuantity 我想輸入數字。 現在我想將啟用的 TextBox 值與 txtQuantity 相乘,並將該值顯示到 txtTotal TextBox 中。 這個怎么做? 同時我想將文本框作為函數的參數傳遞。 所以它會很有用,無論我們想要什么。

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

你能用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);
    });
});

這個 output 將是第一個文本框值 * 數量,因為txtD1是唯一啟用的。

  • 這是假設您正在處理整數。 否則,您可以使用parseFloat
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;
}

上面的 function 將能夠根據文本框狀態(啟用或禁用)和 txtTotal 中的顯示來計算值。 .您可以將此 function 應用於按鈕 事件

@alex,感謝您的通知,我錯過了這個案例。 PFB,修改 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;
}

如果它們是文本區域,您可以使用它來查看它們是否已啟用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM