convert server side vb.net to client side javascript
I've got a function I wrote quite some time ago that works fine, but I'd like to speed up the process and lessen server load by doing the same job开发者_运维技巧 in Javascript.
I seem to be able to GET textbox values ok, but I can't seem to SET textbox values (I'm'-a JS noob). Can anyone lend a hand in converting my VB.NET code to it's JS equivalent?
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
I've got this so far, but I'm not sure how to get much further.
function txtSellingPrice_OnChange() {
var txtSellingPrice = document.getElementById('<%=txtSellingPrice.ClientID %>')
var txtBalanceSheet = document.getElementById('<%=txtBalanceSheet.ClientID %>')
var txtDownPayment = document.getElementById('<%=txtDownPayment.ClientID %>')
}
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;
}
if txtSellingPrice
is a text input for example (<input type="text" />
), to set its value just do:
txtSellingPrice.value = '42';
etc.
To retrieve a value from an input element, do:
var n = txtSellingPrice.value;
Be aware that n will be a string, not a number. Luckily, javascript is pretty lax and will be pretty ready to convert it automatically for you in many circumstances.
You might want to do some validation on the retrieved DOM element too, like:
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
}
精彩评论