使用 jQuery 触发 oninput 事件 [英] Fire oninput event with jQuery

查看:20
本文介绍了使用 jQuery 触发 oninput 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 textarea 上有一个 oninput 事件来检查高度并调整它的大小.现在我有时需要编辑该值.我只是通过在 jQuery 中编辑 val() 来做到这一点,但这不会触发 oninput 事件.有没有办法用jQuery以编程方式触发oninput事件?

I have an oninput event on a textarea to check the height and resize it. Now I need to edit the value sometimes. I do this just by editting the val() in jQuery, but that does not trigger the oninput event. Is there any way to trigger the oninput event programatically with jQuery?

推荐答案

使用 .on('input').例如:

$('textarea').on('input', function() {
  text = $('textarea').val();
  $('div').html(text);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea placeholder="Type something here"></textarea>
<div></div>

这篇关于使用 jQuery 触发 oninput 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆