将服务器端vb.net转换为客户端javascript

时间:2010-03-20 19:39:57

标签: asp.net javascript

我有一段时间我写的功能很好,但是我希望通过在Javascript中完成同样的工作来加快进程并减少服务器负载。

我似乎能够获得文本框值,但我似乎无法设置文本框值(我是一个JS菜鸟)。任何人都可以将我的VB.NET代码转换为它的JS等价物吗?

Protected Sub txtSellingPrice_TextChanged(ByVal sender As Object, ByVal e As EventArgs) _
    Handles txtSellingPrice.TextChanged

    Dim SellingPrice As Double = Double.Parse(txtSellingPrice.Text.Replace("$", ""))
    Dim BallanceSheet As Double = If(txtBalanceSheet.Text = "", 0, Double.Parse(txtBalanceSheet.Text.Replace("$", "")))
    Dim DownPayment As Double = If(txtDownPayment.Text = "", 0, Double.Parse(txtDownPayment.Text.Replace("$", "")))

    txtGoodWill.Text = SellingPrice - BallanceSheet
    txtBalance.Text = SellingPrice - DownPayment
    txtSellingPriceMult.Text = SellingPrice

End Sub

到目前为止,我已经得到了这个,但我不确定如何进一步发展。

function txtSellingPrice_OnChange() {
    var txtSellingPrice = document.getElementById('<%=txtSellingPrice.ClientID %>')
    var txtBalanceSheet = document.getElementById('<%=txtBalanceSheet.ClientID %>')
    var txtDownPayment = document.getElementById('<%=txtDownPayment.ClientID %>')


}

2 个答案:

答案 0 :(得分:1)

function txtSellingPrice_OnChange() {
    //Get your elements
    var txtSellingPrice = document.getElementById('<%=txtSellingPrice.ClientID %>');
    var txtBalanceSheet = document.getElementById('<%=txtBalanceSheet.ClientID %>');
    var txtDownPayment = document.getElementById('<%=txtDownPayment.ClientID %>');

    var txtGoodWill = document.getElementById('<%=txtGoodWill.ClientID %>');
    var txtBalance = document.getElementById('<%=txtBalance.ClientID %>');
    var txtBalance = document.getElementById('<%=txtBalance.ClientID %>');

    //Your if empty value checks
    var sellingPrice = txtSellingPrice.value.replace('$', '');
    sellingPrice = (sellingPrice == '' ? 0 : sellingPrice);
    var ballanceSheet = txtBalanceSheet.value.replace('$','');
    ballanceSheet = (ballanceSheet == '' ? 0 : ballanceSheet);
    var downPayment = txtDownPayment.value.replace('$','');
    downPayment = (downPayment == '' ? 0 : downPayment);

    txtGoodWill.value = (sellingPrice - ballanceSheet);
    txtBalance.value = (sellingPrice - downPayment);
    txtSellingPriceMult.value = sellingPrice;

}

答案 1 :(得分:0)

如果txtSellingPrice是文本输入,例如(<input type="text" />),要设置其值,请执行以下操作:

txtSellingPrice.value = '42';

要从input元素中检索值,请执行:

var n = txtSellingPrice.value;

请注意,n将是一个字符串,而不是一个数字。幸运的是,javascript非常宽松,并且在很多情况下都可以自动为你转换它。

您可能也希望对检索到的DOM元素进行一些验证,例如:

var e0 = document.getElementById('id0');
if (e0) { // the variable is populated OK
  e0.value = 'whatever you want the input to contain';
}
else {
  // element with id 'e0' not found in DOM. log or take some other action
}