Reputation: 1
I have two textboxes and a hidden label.
Normally I do this with asp.net:
After a user enters the number in the first textboxt1
, then after clicking the button getprice
the system multiply the textbox1
value by the hidden label number and show the result in the textbox2
and vice versa.
But I want to achieve this same thing without using a button postback by using either jquery or angularjs.
As the user is typing the figures on the textbox1
, the system should be multiplying that value at runtime and showing it in textbox2
on keypress.
I need a function to this please.
I have tried using this jQuery... Its not working.
I want the function to have a name, so I can put on keypress on textbox1
.
<script type="text/javascript">
$(function () {
$("#<%=Textbox.ClientID%>").bind("input", function() {
var value1 = $("#<%=Textbox.ClientID%>").val()
var value2 = $("#<%=HiddenTextbox2.ClientID%>").val();
$("#<%=Textbox3.ClientID%>").val(value1 * value2);
});
});
</script>
Upvotes: 0
Views: 1006
Reputation: 1
$("#pay,#pre").keyup(function () {
$('#pay').val($('#pay').val() * $('#pre').val());
});
Blockquote
Upvotes: 0
Reputation: 396
I guess it would be easier just to answer using JavaScript. Here's a function that would work:
<script type="text/javascript">
function MultiplyByHidden() {
var value1 = parseFloat(document.getElementById("<%=Textbox.ClientId%>").value)
var value2 = parseFloat(document.getElementById("<%=HiddenTextbox2.ClientId%>").value)
var total = value1 * value2
document.getElementById("<%=Textbox3.ClientId%>").value = total
}
</script>
This function does everything you are looking for and doesn't require importing of extra libraries like JQuery to handle such simple functionality.
Edit I forgot that it would multiply strings too.
Upvotes: 1
Reputation: 33933
I think you try to multiply strings...
The use of parseInt()
or parseFloat()
is recommanded for numbers in text inputs ;)
<script type="text/javascript">
$(function () {
$("#<%=Textbox.ClientID%>").on("input", function() {
var value1 = parseFloat( $("#<%=Textbox.ClientID%>").val() );
var value2 = parseFloat( $("#<%=HiddenTextbox2.ClientID%>").val() );
$("#<%=Textbox3.ClientID%>").val(value1 * value2);
});
});
</script>
Upvotes: 0