Javascript 如何从输入类型文本中获取旧值和新值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28250520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get old value and new value from input type text
提问by Shojib Flamon
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();
}
回答by super
回答by lasbreyn
You may add another attribute (prevvalue) in your input fields like so:
您可以在输入字段中添加另一个属性(prevvalue),如下所示:
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.
然后,无论何时发生任何更改,您都可以访问这两个值。您需要预先填充旧值或在新值更改时更新它。干杯。
回答by Felipe Diniz
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');
})
})
回答by Md. Ashaduzzaman
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;
}

