使用jQuery触发oninput事件

cnwbcb6i  于 2022-11-22  发布在  jQuery
关注(0)|答案(7)|浏览(246)

我在一个文本区域上有一个oninput事件,用来检查高度和调整大小。现在我有时需要编辑值。我只是通过编辑jQuery中的val()来实现这一点,但这并没有触发oninput事件。有没有办法用jQuery通过编程来触发oninput事件?

7qhs6swi

7qhs6swi1#

使用.on('input')。例如:
第一个

m528fe3b

m528fe3b2#

这有点太晚了,但为了将来的参考,有.trigger方法。
第一个

bnlyeluc

bnlyeluc3#

您可以简单地调用它,例如:

$("input")[0].oninput = function () {
   alert("hello"); 
};

$("input")[0].oninput();

......但是正如@Sammaye指出的,jQuery没有显式的“oninput”处理程序,所以您必须使用POJS。
Demo on JS Fiddle

ppcbkaq5

ppcbkaq54#

oninput 实际 上 还 没有 在 JQuery 中 。
你 可以 在 这里 看到 关于 它 的 帖子 :
http://forum.jquery.com/topic/html5-oninput-event 的 最 大 值
http://bugs.jquery.com/ticket/9121 格式
基本 上 , 普遍 的 共识 是 , 他们 还 不 想要 它 。
但是 不 , 直接 更改 val ( ) 不会 触发 html5 oninput , 因为 它 的 规范 规定 , 当 用户 在 UI 中 更改 输入 的 值 时 , 它 会 触发 。
编辑 :
然而 , 有人 已经 善意 地 做 了 一 个 插件 的 人 谁 希望 使用 HTML5 只 事件 :https://github.com/dodo/jquery-inputevent 格式

disho6za

disho6za5#

您可以绑定到输入并更改:

输入将在用户输入时触发
更改将在change()和to瓦尔(““)赋值时触发,但在某些更改之后

$("#textarea").bind("input change", function() {
    alert("change happend");
});
...

绑定到change后,您可以在每次瓦尔(““)赋值时手动调用它:

$("#textarea").val("text").change();

或者您可以覆盖jQuery瓦尔(““)方法,以便在每次用户val(““)调用时触发change

(function ($) { var fnVal = $.fn.val;
    $.fn.val = function(value) {
        if (typeof value == 'undefined') {
            return fnVal.call(this);
        }
        var result = fnVal.call(this, value);
        $.fn.change.call(this); // calls change()
        return result;
    };
})(jQuery);
f8rj6qna

f8rj6qna6#

尝试使用“keypress”或“keydown”。
例一:

$("#textarea").keypress(function(){
    alert($("#textarea").val());
});

例二:

$("#textarea").keydown(function(){
    alert($("#textarea").val());
});
hvvq6cgz

hvvq6cgz7#

按 下 RUN CODE SNIPPET 查看 结果

    • 我 一直 在 寻找 一 个 更 好 的 例子 来 连接 一 个 输入 范围 到 一 个 输入 值 , 所以 * * * 我 在 一 个 JQuery 插件 中 修改 了 Fernando 的 例子 * , 所以 :每 一 次 你 都会 得到 他 价值* * <input type="text" disabled id="value" class="range1" value="0"> * * 因此 类似 于 任何 父 范围 * * id = " range1 " * * 存在 一 个 子 id = " value " * * class = " range1 " * *
<!-- <script src="../js/jquery.js"></script> -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>


1<input type="range" min="1" max="100" value="50" id="range1"><input type="text" disabled id="value" class="range1" value="0"><br>
2<input type="range" min="1" max="100" value="50" id="range2"><input type="text" disabled id="value" class="range2" value="0"><br>
3<input type="range" min="1" max="100" value="50" id="range3"><input type="text" disabled id="value" class="range3" value="0"><br>
4<input type="range" min="1" max="100" value="50" id="range4"><input type="text" disabled id="value" class="range4" value="0"><br>
...<br>
n<input type="range" min="1" max="100" value="50" id="rangen"><input type="text" disabled id="value" class="rangen" value="0"><br>


<script type="text/javascript">
<!--
$(document).ready(function() {
  $('input').on("input",function(){
    $('input').each(function(index) {
      //console.log('1 '+index + ': ' + $(this).text()+',id='+$(this).attr('id'));
      if($(this).attr('id')=='value'){
        //console.log('2 class='+$(this).attr('class'));
        var obj=$('input#'+$(this).attr('class') );
        var hisvalue=$(this);
        //console.log('3 parent`s value='+obj.val() );
        obj.on("input",function(){
          hisvalue.val(obj.val());
        });
      }
    });
  });
  $('input').trigger("input");
});
//-->
</script>

中 的 每 一 个

相关问题