i am writing a function which will be called when a key is down on keyboard how to access value of that textbox in which key is pressed. my code is
function isValid(evt,that){
console.log('ing to this 1');
var charCode = (evt.which) ? evt.which : event.keyCode;
console.log(that.val());
return true;
}
$(document).on('keydown','.is_valid',isValid);
how to get the value of textbox which is currently getting input from keyboard ?? please guideline How to acplish this
i am writing a function which will be called when a key is down on keyboard how to access value of that textbox in which key is pressed. my code is
function isValid(evt,that){
console.log('ing to this 1');
var charCode = (evt.which) ? evt.which : event.keyCode;
console.log(that.val());
return true;
}
$(document).on('keydown','.is_valid',isValid);
how to get the value of textbox which is currently getting input from keyboard ?? please guideline How to acplish this
Share Improve this question asked Jul 12, 2013 at 9:00 mathlearnermathlearner 7,65932 gold badges131 silver badges191 bronze badges5 Answers
Reset to default 5I'd suggest:
function isValid(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode,
self = evt.target;
console.log(self.value);
return true;
}
$(document).on('keydown', '.is_valid', isValid);
JS Fiddle demo.
So long as the event is available to the function, you can access the target of that event with the appropriately-named event.target
. If you need that to be a jQuery object, with access to jQuery methods you can use: $(event.target)
.
You could also use:
self = evt.currentTarget;
JS Fiddle demo.
Or:
self = document.activeElement;
JS Fiddle demo.
References:
document.activeElement
.event.currentTarget
.event.target
.
this
inside the event handler will point to the targeted element
function isValid(evt,that){
var charCode = (evt.which) ? evt.which : event.keyCode;
console.log('log', $(this).val(), charCode);
return true;
}
$(document).on('keydown','.is_valid',isValid);
Demo: Fiddle
you can get the current element using $(this)
$(document).on('keydown','.is_valid',isValid){
alert($(this).val());
//and do stuff here
}
change that.val()
to $(this).val()
function isValid(evt) {
console.log('ing to this 1');
var charCode = (evt.which) ? evt.which : event.keyCode;
console.log($(this).val()); // change that to $(this)
return true;
}
$(document).on('keydown', '.is_valid', isValid);
Working good. Check JSFiddle
There are a few different ways to get this. Below are three examples:
function isValid(evt) {
// if you use 'this', you don't need to pass 'evt' to the function
console.log(this.value);
// you can reference the event target
console.log(evt.target.value);
// you can reference the event's current target
console.log(evt.currentTarget.value);
return true;
}
$(document).on('keydown', '.is_valid', isValid);
These will all give you the value of the textbox.
If you want to see what other options might be available, console.log(evt);
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742301377a4418094.html
评论列表(0条)