我有我想能够提取其值的值改变时的形式内的输入元件。 看看这个小提琴: http://jsfiddle.net/spryno724/WaBzW/1/ 。
通过使用change
情况下,我能够从显示在输入失去焦点的文本输入的值。 然而,当表单被重置或JavaScript时清除文本输入的值值不更新。
有没有办法,我能听,当任何变化是一个文本输入控件制作将派出一个特定的事件?
我想避免的变通办法,例如侦听当表单复位,或当按下清除按钮 。 这是我的应用程序的功能简化的例子,它将获得疯狂的非常快,如果我尝试做了这一切。
感谢您的时间。
$(document).ready(function() {
$('input.clear').click(function() {
$('input.input').val('');
$('p.display').text('The value of the text input is: ');
});
$('input.input').on('keyup change', function() {
$('p.display').text('The value of the text input is: ' + $(this).val());
});
})
DEMO 1
也许这个解决方案可以帮助你:
$(document).ready(function() {
$('input.clear, input[type=reset]').on('click', function() {
$('input.input').val('').change();
$('p.display').text('The value of the text input is: ');
});
$('input.input').on('keyup change', function() {
$('p.display').text('The value of the text input is: ' + $(this).val());
});
});
DEMO 2
这个问题的另一个完全相同的副本。 请参阅使用的上票的回答我的问题最多的答案:
JS事件:在文字输入上的值更改事件挂钩
覆盖val
jQuery的方法
HTML
<input id='myInput' name='myInputField' value='v1'>
JS
var myInput = $('#myInput');
//this will not trigger the change event
myInput.val('v2');
//override val method
var oldVal = myInput.val;
myInput.val = function(value){
var returnVal = oldVal.call(this,value);
myInput.change();
return returnVal;
}
// this will trigger the change event
// myInput.val is overridden
myInput.val('v3');
注意这只会工作,如果VAL方法是从所谓的myInput
变量
尝试
$('#input').live('change',function() { })