Title: On keypress of jquery change the value of textbox
Tags: jquery;asp.net-mvc
Question: I got the JSON data from my ASP.net MVC Controller, and loop it to display in my View.
$(document).ready(function () {
var amount = 0;
var subTotal = 0;
var vat = 0;
var total = 0;
var qty = 0;
var urlSession = '<%: Url.Content("~/") %&gt;' + "Quotation/ProductInSession";
$.getJSON(urlSession, function (dataQuote) {
var i = dataQuote.ja.length;
$.each(dataQuote.ja, function (index, data) {
amount = data.ProductQty * data.ProductPrice;
subTotal += amount;
$("#listProduct tr#first_row").after('<tr style="height:25px;"&gt;<td class="tablecell2" width="40px" align="center"&gt;' + i + '</td&gt;<td class="tablecell2"&gt;' + data.ProductName + '</td&gt;<td class="tablecell2" width="70px" align="center"&gt;<input id="qty" type="text" value="' + data.ProductQty + '" size="2"/&gt;</td&gt;<td class="tablecell2" width="90px" align="right" style="padding-right:5px;"&gt;' + data.ProductPrice + '</td&gt;<td class="tablecell2" style="border-right:1px solid silver;padding-right:5px;" width="120px" align="right"&gt;<span id="amount"&gt;' + amount + '</span&gt;</td&gt;</tr&gt;');
i--;
$('#qty').keypress(function (event) {
var keycode = (event.keyCode ? event.keyCode : event.which);
if (keycode == '13') {
qty = $("#qty").val();
$("#qty").html(qty);
amount = qty * data.ProductPrice;
$("#amount").html(amount);
}
});
});
vat = (subTotal * 10) / 100;
total = subTotal + vat;
$("#vvat").html("<b&gt;" + vat + "</b&gt;");
$("#ssubtotal").html(subTotal);
$("#sgrandtotal").html("<b&gt;" + total + "</b&gt;");
What I want to do is :
Change the value in textbox when I press the key Enter
Change the value of the amount (amount = qty * UnitPrice)
Should I create another action of the controller to handle this update or anything beside this.
Thanks for any help anyone can give me!


Here is another answer: try this
$('#qty').bind("keydown",function (e) {
if (e.which == 13) {
e.preventDefault();
qty = $("#qty").val();
$("#qty").html(qty);
amount = qty * data.ProductPrice;
}
});
Comment for this answer: Note incase OP doesn't know: `e.which == 13` is a check to see if the keycode for the event is 13, which corresponds to the enter key.
Comment for this answer: It may be a browser issue.. For getting the key code try this line from your code itself var keycode = (event.keyCode ? event.keyCode : event.which);

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:10/09/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<jquery><asp.net-mvc>', 'Title': 'On keypress of jquery change the value of textbox', 'ViewCount': 2550, 'id': 12791779, 'user_AboutMe': '<p>Developer</p>\n', 'user_AccountId': 463634.0, 'user_DisplayName': 'Nothing', 'user_Reputation': 2626.0, 'user_Views': 597.0, 'user_id': 866659.0}}