我使用的是 html 表单,其标记为date
。我想在调用 .change
后检查旧值
我的代码:
$('#date').change(function(event) {
event.preventDefault();
$('#list_data').html('');
$('#btndownload').hide();
$('#btngenerate').hide();
val = $(this).val(); //want to change it to old/previous value
if(val || val !=""){
console.log("this value: " +val);
$('#btnview').show();
}
else{
$('#btnview').hide();
console.log("closed");
}
});
有什么线索吗?
最佳答案
您可以将旧值保留在缓存中,该缓存可以使用 .data()
进行操作方法。
此外,如果需要,可以使用 defaultValue
属性来获取创建此对象的 HTML 中最初指定的默认值。可以使用 .prop()
获取它方法
$('#date').change(function(event) {
var currentValue = $(this).val();
var previousValue = $(this).data('previousValue');
//if you want to get original value set in html
var defaultValue = $(this).prop('defaultValue');
//set currentValue as previousValue
$(this).data('previousValue', currentValue);
console.clear();
console.log("previousValue: " + previousValue);
console.log("currentValue:" + currentValue);
console.log("defaultValue: " + defaultValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="date" value="2020-01-21">
关于javascript - 从变更事件中获取旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59834351/