saving value of textbox on keypress event
Good morning to everyone. Thanks for visiting my question. How can I save value of textbox on enter key and, when user presses up key, so that when user press the up arrow key, it loads the value of the last content in the textbox.
it here my code so far, but it doesnt work unfortunately..
$(document).ready开发者_如何学C(function () {
var lstMsg;
$('#myTextbox').bind("keyup", function(e){
if ( e.keyCode == 39){
$(this).val($(lstMsg).val());
}
if ( e.keyCode == 13){
lstMsg.val($(this).val());
}
});
});
Thank you for your patience.
lstMsg
being a variable does not have a .val()
method defined on it.
$(document).ready(function () {
var lstMsg;
$('#myTextbox').bind("keyup", function(e){
if ( e.keyCode == 39){
$(this).val(lstMsg);
}
if ( e.keyCode == 13){
lstMsg = $(this).val();
}
});
});
Store and get the textarea value using data()
to store the value
$(this).data('x',this.value);
To get the stored value
$(this).data('x');
I Think Best way is this :
$(document).ready(function ()
{
$('#myTextbox').bind("keyup", function(e)
{
if ( e.keyCode == 38)
{
$(this).val($(this).data('last_val'));
}
if ( e.keyCode == 13)
{
$(this).data('last_val',$(this).val())
}
});
});
Demo : http://jsfiddle.net/Davood/cdzWH/
精彩评论