嗨,
我需要在gridview中计算两个textboxex的值,并在第二个文本框中输入值后立即使用javascript在第三个文本框中显示结果。
我的文本框字段是:数量和价格
结果应显示在总计中。
也就是说,(总)值=数量*费率。
我已经试过了
后面的代码:C#
protected void gvPOItms__RowCreated(Object sender, GridViewRowEventArgs e)
{
try
{
TextBox txt1 = (TextBox)e.Row.FindControl("txtQty");
TextBox txt2 = (TextBox)e.Row.FindControl("txtRate");
TextBox txt3 = (TextBox)e.Row.FindControl("txtValue");
txt1.Attributes["onKeyup"] = "javascript: return multiplication('" + txt1.ClientID + "','" + txt2.ClientID + "','" + txt3.ClientID + "')";
txt2.Attributes["onKeyup"] = "javascript: return multiplication('" + txt1.ClientID + "','" + txt2.ClientID + "','" + txt3.ClientID + "')";
}
catch (Exception ex)
{
Response.Write(ex);
}
}
JAVASCRIPT:
<Script type="text/javascript">
function multiplication(tx1,txt2,txt3)
{
//Your logic for multiplication
var Qty=document.getElementById(txt1).value;
var Rate=document.getElementById(txt2).value;
document.getElementById(txt3).value=Qty*Rate;
}
</script>
标记页:
但是,我无法得到答案,它没有抛出任何错误。
有人可以告诉我怎么了吗?
最佳答案
您应该在乘法之前将字符串更改为int(因为任何文本框的值都返回字符串):)
尝试这个 :-
txt1.Attributes["onKeyup"] = "javascript: return multiplication('" + Convert.ToInt32(txt1.Text) + "','" + Convert.ToInt32(txt2.Text) + "','" + Convert.ToInt32(txt3.ClientID) + "')";
txt2.Attributes["onKeyup"] = "javascript: return multiplication('" + Convert.ToInt32(txt1.Text) + "','" + Convert.ToInt32(txt2.Text) + "','" + Convert.ToInt32(txt3.ClientID) + "')";
在脚本中:-
<Script type="text/javascript">
function multiplication(Qty,Rate,txt3)
{
//Your logic for multiplication
document.getElementById(txt3).value=Qty*Rate;
}
</script>