Javascript Jquery,获取用户控件内的标签值
我需要在asp.net页面中使用usercontrol标签中的值进行计算 用户控件标签为:Javascript Jquery,获取用户控件内的标签值,javascript,jquery,asp.net,jquery-selectors,Javascript,Jquery,Asp.net,Jquery Selectors,我需要在asp.net页面中使用usercontrol标签中的值进行计算 用户控件标签为: <asp:Label ID="LblInvoicePriceValue" runat="server" ></asp:Label> 我将其包括如下内容: <Controls:VehicleInformation ID="VehicleInformationControl" runat="server" /> 我的jquery函数类似于: 请参见第1点和第2
<asp:Label ID="LblInvoicePriceValue" runat="server" ></asp:Label>
我将其包括如下内容:
<Controls:VehicleInformation ID="VehicleInformationControl" runat="server" />
我的jquery函数类似于:
请参见第1点和第2点
<script type="text/javascript">
$(document).ready(function () {
alert('call function to do calculation here');
// 1. Find in the vehicle information user control the invoiced ammount label
// 2. Find the vat excluded value **after** it was typed in the textbox
// 3. If invoiced ammount is greater than zero, then
// 3.a Find Label Percentage
// 3.b Label.Text = (AmmountWithoutVat/InvoicedAmmount)*100 + '%'
});
</script>
$(文档).ready(函数(){
警报(“在此调用函数进行计算”);
//1.在车辆信息中查找用户控制发票金额标签
//2.在文本框中输入**后查找增值税排除值**
//3.如果发票金额大于零,则
//3.查找标签百分比
//3.b Label.Text=(无增值税/发票金额)*100+'%
});
生成的HTML:UPdate1
对于标签:
<span id="MainContent_VehicleInformationControl_LblInvoicePriceValue" class="bold"></span>
对于文本框:
<input name="ctl00$MainContent$TxtVatExcluded" type="text" id="TxtVatExcluded" class="calculation" />
更新2:
<script type="text/javascript">
$(document).ready(function () {
alert('call function to do calculation here');
$("#TxtVatExcluded").keypress(function() {
var invoiceprice = $("#MainContent_VehicleInformationControl_LblInvoicePriceValue").text();
var vatexcluced = $("#TxtVatExcluded").val();
var lblPercentage = $("#MainContent_LblPercentage");
if (invoiceprice > 0) {
lblPercentage.text((vatexcluced / invoiceprice) * 100);
}
})
// 1. Find in the vehicle information user control the invoiced ammount label
// 2. Find the vat excluded value after it was typed in the textbox
// 3. If invoiced ammount is greater than zero, then
// 3.a Find Label Percentage
// 3.b Label.Text = (AmmountWithoutVat/InvoicedAmmount)*100 + '%'
});
</script>
$(文档).ready(函数(){
警报(“在此调用函数进行计算”);
$(“#TxtVatExcluded”).keypress(函数(){
var invoiceprice=$(“#主要内容"车辆信息控制"LblInvoicePriceValue”).text();
var VatExclused=$(“#txtVatExclused”).val();
var lblPercentage=$(“主要内容lblPercentage”);
如果(发票价格>0){
lbl百分比文本((增值税除外/发票价格)*100);
}
})
//1.在车辆信息中查找用户控制发票金额标签
//2.在文本框中键入增值税排除值后查找该值
//3.如果发票金额大于零,则
//3.查找标签百分比
//3.b Label.Text=(无增值税/发票金额)*100+'%
});
您可以使用元素的呈现ID来使用jQuery获取值
var lbl = $("#MainContent_VehicleInformationControl_LblInvoicePriceValue").text();
var tbox = $("#TxtVatExcluded").val();
稍后,当计算完成时,可以将标签文本更新为
$("#MainContent_VehicleInformationControl_LblInvoicePriceValue").html("new label");
更新:
要使用用户键入的逻辑,必须将函数绑定到keypress
/keypup
/keypdown
事件
$("#myinputbox").keypress(function() {
var lbl = $("#MainContent_VehicleInformationControl_LblInvoicePriceValue").text();
var tbox = $("#TxtVatExcluded").val();
//... so on
}
更新2:
由于您试图使用这些值进行计算,因此更安全的做法是确保首先有数字。为此,您可以根据需要使用parseInt()
,parseFloat()
$("#TxtVatExcluded").keypress(function() {
var invoiceprice = $("#MainContent_VehicleInformationControl_LblInvoicePriceValue").text();
var vatexcluced = $("#TxtVatExcluded").val();
var lblPercentage = $("#MainContent_LblPercentage");
if (invoiceprice > 0) {
lblPercentage.text((parseInt(vatexcluced) / parseInt(invoiceprice)) * 100);
}
})
更新
如果要检查文本字段是否为空,请仅复制标签,然后使用以下代码
var label_text = $("#MainContent_VehicleInformationControl_LblInvoicePriceValue").text();
var txt = $("#TxtVatExcluded").val();
if(txt.length==0)
{
$("#TxtVatExcluded").val(label_text);
}
这将获得label控件的值:
function Calculate()
{
var InvoicedAmmount = $("#MainContent_VehicleInformationControl_LblInvoicePriceValue").text();
var AmmountWithoutVat = $("#TxtVatExcluded").val();
var Result = (AmmountWithoutVat/InvoicedAmmount)*100
$("#OutputLabel").html(Result + " %");
}
您可以将和onBlur事件附加到您的文本框,以便在他们离开文本框时触发您的计算-您不会真的希望在他们键入时重新计算金额
$(document).ready(function ()
{
$("#TxtVatExcluded").bind("blur",function(){ Calculate(); });
}
您是否可以包含控件/标签生成的实际HTML输出(使用视图源代码就足够了)…如果您已经阅读jQuery大约一两个小时了,这应该不会太难$(“#theID”).val()将获得该id的值,$(“#theID”).blur(函数(){})将允许您定义焦点离开该字段时发生的情况(假设theID是一个输入元素)。以及如何使我的逻辑仅在用户在文本框中键入内容后执行
$(document).ready(function ()
{
$("#TxtVatExcluded").bind("blur",function(){ Calculate(); });
}