将onchange事件侦听器添加到html输入字段 [英] Add an onchange event listener to an html input field

查看:64
本文介绍了将onchange事件侦听器添加到html输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在没有jquery的情况下向这些输入字段添加 onchange 事件:

I would like to add an onchange event to those input fields without jquery:

<input type="text" id="cbid.wizard.1._latitude">
<input type="text" id="cbid.wizard.1._longitude">

我已经可以使用来调用对象了

I can already call the object with

<script type="text/javascript">
    alert(document.getElementById('cbid.wizard.1._latitude').id);
</script>

最后,我想添加这种行为,即如果您在第一个输入中输入一对坐标,我会将该对分布在两个输入字段中吗?

In the end, I want to add this behaviour, that if you enter a pair of coordinates into the first input, I will spread the pair over the two input fields?

如何使用javascript添加 onchange 事件?

How do I add an onchange event with javascript?

推荐答案

嗯,为'change'事件附加事件处理程序吗?

Ummm, attach an event handler for the 'change' event?

纯JS

document.getElementById('element_id').onchange = function() {
  // your logic
};

// or

document.getElementById('element_id').addEventListener(
  'change',
  callbackFunction,
  false
);

jQuery

$('#element_id').change(function() {
  // your logic
});

注意

请注意,文本字段中的 change 事件将在 blur 事件之后触发.您可能正在寻找 keypress 事件之类的东西.

Note

Note, that change event on the text field will be fired after the blur event. It's possible that your looking for keypress event's or something like that.

这篇关于将onchange事件侦听器添加到html输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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