Ezr*_*ab_ 1 javascript jquery dom-events jquery-events
这个问题之前可能已被问过并回答了一千次,但我不知道是不是因为SEO,我找不到合适的解决方案.我在Chrome中获得未定义或空白文本.
$('input[type="text"]').on('input', (a, b) => {
console.log(this.value);
});
Run Code Online (Sandbox Code Playgroud)
这仍然在控制台中返回undefined我也尝试过:
$('input[type="text"]').on('input', (a, b) => {
console.log(b);
});
$('input[type="text"]').on('input', (a, b) => {
console.log($(this).val());
});
$('input[type="text"]').on('input', (a, b) => {
console.log(a.value);
});
$('input[type="text"]').on('input', (a, b) => {
console.log(b.value);
});
Run Code Online (Sandbox Code Playgroud)
任何想法都将非常感激.
this由于您使用es6箭头函数格式将改变上下文,e.target而不是使用它,因此该关键字对您无法预期.
$('input[type="text"]').on('input', (e) => {
console.log(e.target.value);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="test">Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |