简体   繁体   English

价值变化启动活动

[英]Launch event on value change

I made simple date picker using javascript and jquery. 我使用javascript和jquery制作了简单的日期选择器。 After choosing date it is shown in input box. 选择日期后,它会显示在输入框中。 This input boxis not lauching change event, probably because it was changed using javascript. 此输入框未启动更改事件,可能是因为已使用javascript对其进行了更改。 Is there any way to launch this event, or to make custom one? 有什么方法可以启动此活动,也可以进行自定义活动吗?

分配新值后运行以下代码:

$('input').change();

.change just points to the .on function inside jquery, so it's better to use the .on directly. .change只是指向jquery中的.on函数,因此最好直接使用.on。

$('#inputID').on('change', function() {
});
$("input#yourInputId").change();

or 要么

$("input#yourInputId").trigger("change");

Should do the trick. 应该做到的。 Replace the #yourInputId with an id that represents the ID="abc" part of your HTML for the textbox 将#yourInputId替换为代表文本框HTML的ID="abc"部分的ID="abc"

  • Be aware of potential browser irregularities or lack of support for change events - behaviour may vary, particularly in older browsers. 请注意潜在的浏览器违规行为或缺少对更改事件的支持-行为可能会有所不同,尤其是在较旧的浏览器中。

There is a change and an input event. 有一个更改和一个输入事件。 The first fires after an input changed and lost focus. 输入更改并失去焦点后,第一个触发。 The latter fires immediately when the input changes. 当输入更改时,后者立即触发。 However there are no events that fire after programatically changing an input. 但是,以编程方式更改输入后不会触发任何事件。

If you have control over the code that changes your inputs you can of course trigger the events manually like described in the other answers. 如果您可以控制更改输入的代码,则当然可以像其他答案中所述手动触发事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM