如何在文本框中使用 onchange() 事件获取旧值 [英] How to get old Value with onchange() event in text box

查看:90
本文介绍了如何在文本框中使用 onchange() 事件获取旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本输入.页面加载时会填充一个值.如果用户在文本框中更改了任何内容,那么我想获取更改后的值(新值)和旧值.但是调用 ELEMENT.value 它只返回更改的/新值.

如何获取旧值?

这是我的代码:

 <script type="text/javascript">函数 onChangeTest(changeVal) {警报(值是"+ changeVal.value);}<身体><表格><div><输入类型=文本"id=测试"值=ABS"onchange="onChangeTest(this)">

</表单>

解决方案

element.defaultValue 会给你原始值.

请注意,这只适用于初始值.

如果您需要在每次更改时保留旧"值,则 expando 属性或类似方法将满足您的需求

I have a text input. A value is populated into it when the page loads. If the user changes anything in text box, then I want to get the changed value (the new value) and old value. But calling ELEMENT.value it only returns the changed/new value.

How do I get the old value?

Here is my code:

      <head>
        <script type="text/javascript">
          function onChangeTest(changeVal) {
            alert("Value is " + changeVal.value);
          }
        </script>
      </head>
      <body>
        <form>
          <div>
              <input type="text" id="test" value ="ABS" onchange="onChangeTest(this)">  
          </div>
        </form>
      </body>

解决方案

element.defaultValue will give you the original value.

Please note that this only works on the initial value.

If you are needing this to persist the "old" value every time it changes, an expando property or similar method will meet your needs

这篇关于如何在文本框中使用 onchange() 事件获取旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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