简体   繁体   中英

How to get old value and new value from input type text

How to get old value and new value from input type text;

<input type="text" name="quantity-row_111" value="1" id="quantity-row_111" onchange="myfunction(id);">

function myfunction(id){
     var oldvalue = getoldvalue();
     var newvalue = getnewvalue();
}

Try this,

HTML

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

JavaScript

function onChangeTest(textbox) {
      console.log("New value: " + textbox.value + "\n" + "Old value: " + textbox.oldvalue);
}

Reference

You may add another attribute (prevvalue) in your input fields like so:

 function myfunction(id){ var oldvalue = id.getAttribute("prevvalue"); var newvalue = id.value; alert(oldvalue); alert(newvalue); }
 <input type="text" name="quantity-row_111" value="1" prevvalue="4.50" id="quantity-row_111" onchange="myfunction(this);">

Then you have access to both values whenever any change occurs. You will need to either pre-populate the old value or update it whenever the new value changes. Cheers.

If your element id is quantity-row_111, you can get the original value and the new value by :

console.log("new value="+ document.getElementById('quantity-row_111').value  ); 
console.log("Original value="+ document.getElementById('quantity-row_111').defaultValue);

Look at the console log for the result.

Keep the newvalue saved as your oldvalue. This might be what you what you want,

var oldvalue = document.getElementById("quantity-row_111").value;
var newvalue;
function myfunction(id){
    alert(oldvalue);
    newvalue = document.getElementById("quantity-row_111").value;
    oldvalue = newvalue;
}

jsFiddle

you should try something like this:

$(document).ready(function () {
        var OldValue;
        $("form :input").click(function () {
            var field= ($(this).attr('name'));
            OldValue= ($(this).attr('field'));
        })


        $("form :input").change(function () {
            var field = ($(this).attr('name'));
            var value = ($(this).attr('value'));
            console.log('Field:: ' + field + '\nNew Value: ' + value + '\nOld Value: ' + OldValue+'\n--------------------\n');

        })
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM