html< input type =" text" /> onchange事件不起作用 [英] html <input type="text" /> onchange event not working

查看:77
本文介绍了html< input type =" text" /> onchange事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些实验。我想要发生的是,每次用户在文本框中键入内容时,它都会显示在对话框中。我使用 onchange 事件属性来实现它,但它不起作用。我仍然需要按下提交按钮才能使其正常工作。我读到了关于AJAX的内容,我正在考虑了解这一点。我是否仍然需要AJAX才能使其工作或简单的JavaScript足够?请帮忙。

I am trying to do some experiment. What I want to happen is that everytime the user types in something in the textbox, it will be displayed in a dialog box. I used the onchange event property to make it happen but it doesn't work. I still need to press the submit button to make it work. I read about AJAX and I am thinking to learn about this. Do I still need AJAX to make it work or is simple JavaScript enough? Please help.

index.php

<script type="text/javascript" src="javascript.js"> </script>

<form action="index.php" method="get">
 Integer 1: <input type="text" id="num1" name="num1" onchange="checkInput('num1');" /> <br />
 Integer 2: <input type="text" id="num2" name="num2" onchange="checkInput('num2');" /> <br />
 <input type="submit" value="Compute" />
</form>

javascript.js

function checkInput(textbox) {
 var textInput = document.getElementById(textbox).value;

 alert(textInput); 
}


推荐答案

onchange 仅在控件模糊时触发。请尝试 onkeypress

onchange is only triggered when the control is blurred. Try onkeypress instead.

这篇关于html&lt; input type =&quot; text&quot; /&GT; onchange事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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