2010-03-20 58 views
2

我有一個函數,我很早前寫了很好的工作,但我想通過在Javascript中執行相同的工作來加快進程並減少服務器負載。轉換服務器端vb.net到客戶端javascript

我似乎能夠獲取文本框值好,但我似乎無法設置文本框的值(我是一個JS noob)。任何人都可以將我的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 %>') 


} 

回答

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; 

} 
0

如果txtSellingPrice是例如(<input type="text" />)文本輸入,來設定其值只是做:

txtSellingPrice.value = '42'; 

要檢索從輸入元素的值,如下:

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 
} 
相關問題