gpt4 book ai didi

javascript - 在模糊事件不起作用时给出错误

转载 作者:行者123 更新时间:2023-11-29 19:50:47 25 4
gpt4 key购买 nike

在模糊事件不工作时给出下面写的错误。我只是想将两个文本框中的值相乘。我做错了什么。

 TypeError: document.getElementById(...) is null .  
var txt8 = document.getElementById("txtQuantity").value;

<script type="text/javascript">
function CalcSellPrice2() {

var txt8 = document.getElementById("txtQuantity").value;
var txt9 = document.getElementById("txtUnit").value;
document.getElementById("TextBox10").value = txt8 + txt9;
document.getElementById(txtTotal).innerHTML = TotalPriceValue;
}


</script>

<asp:TextBox ID="txtQuantity" runat="server" CssClass="span6"
onblur="CalcSellPrice2();"></asp:TextBox>

最佳答案

由于 TextBoxasp.net 控件,因此您必须使用 ClientID 才能访问您的 asp 控件,因为 asp .net rander HTML with markup 并将每个父命名容器的 ID 值与控件的 ID 值连接起来(引用 THIS )作为

<%= txtQuantity.ClientID %>

所以你的脚本是这样的

var txt8 = document.getElementById(" <%= txtQuantity.ClientID %>").value;

访问 ASP.NET 控件的另一种方法是使用属性 ClientIDMode,一旦将其设置为 Static,它就不会连接ID 每个父命名容器的值。所以您的 HTML 将如下所示。

<asp:TextBox ClientIDMode="Static" ID="txtQuantity" runat="server" CssClass="span6" onblur="CalcSellPrice2();"></asp:TextBox>

通过这种方式,您可以简单地访问您在问题中使用的 asp 控件,而无需使用 ClientID

关于javascript - 在模糊事件不起作用时给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17872942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com