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

后端 未结 9 2172
一向
一向 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:25

    I am not sure, but maybe this logic would work.

    var d = 10;
    var prevDate = "";
    var x = 0;
    var oldVal = "";
    var func = function (d) {
        if (x == 0 && d != prevDate && prevDate == "") {
            oldVal = d;
            prevDate = d;
        }
        else if (x == 1 && prevDate != d) {
            oldVal = prevDate;
            prevDate = d;
        }
        console.log(oldVal);
        x = 1;
    };
    /*
             ============================================
             Try:
             func(2);
             func(3);
             func(4);
    */
    
    0 讨论(0)
  • 2020-11-28 06:26

    You'll need to store the old value manually. You could store it a lot of different ways. You could use a javascript object to store values for each textbox, or you could use a hidden field (I wouldn't recommend it - too html heavy), or you could use an expando property on the textbox itself, like this:

    <input type="text" onfocus="this.oldvalue = this.value;" onchange="onChangeTest(this);this.oldvalue = this.value;" />
    

    Then your javascript function to handle the change looks like this:

        <script type="text/javascript">
        function onChangeTest(textbox) {
            alert("Value is " + textbox.value + "\n" + "Old Value is " + textbox.oldvalue);
        }
        </script>
    
    0 讨论(0)
  • 2020-11-28 06:27

    A dirty trick I somtimes use, is hiding variables in the 'name' attribute (that I normally don't use for other purposes):

    select onFocus=(this.name=this.value) onChange=someFunction(this.name,this.value)><option...
    

    Somewhat unexpectedly, both the old and the new value is then submitted to someFunction(oldValue,newValue)

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