How to get old Value with onchange() event in text box

后端 未结 9 2171
一向
一向 2020-11-28 05:49

I have a text Box and a Value poplulated in it when page loads. Now If user chanegs any thing in text box, then I want to get the Changed Value(New Value) and Old Value but

相关标签:
9条回答
  • 2020-11-28 06:04

    Maybe you can store the previous value of the textbox into a hidden textbox. Then you can get the first value from hidden and the last value from textbox itself. An alternative related to this, at onfocus event of your textbox set the value of your textbox to an hidden field and at onchange event read the previous value.

    0 讨论(0)
  • 2020-11-28 06:06

    You should use HTML5 data attributes. You can create your own attributes and save different values in them.

    0 讨论(0)
  • 2020-11-28 06:18

    You can do this: add oldvalue attribute to html element, add set oldvalue when user click. Then onchange event use oldvalue.

    <input type="text" id="test" value ="ABS" onchange="onChangeTest(this)" onclick="setoldvalue(this)" oldvalue="">
    
    <script>
    function setoldvalue(element){
       element.setAttribute("oldvalue",this.value);
    }
    
    function onChangeTest(element){
       element.setAttribute("value",this.getAttribute("oldvalue"));
    }
    </script>
    
    0 讨论(0)
  • 2020-11-28 06:19

    Maybe you can try to save the old value with the "onfocus" event to afterwards compare it with the new value with the "onchange" event.

    0 讨论(0)
  • 2020-11-28 06:20

    I would suggest:

    function onChange(field){
      field.old=field.recent;
      field.recent=field.value;
    
      //we have available old value here;
    }
    
    0 讨论(0)
  • 2020-11-28 06:23

    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

    0 讨论(0)
提交回复
热议问题