javascript - 从更改事件中获取旧值
问题描述
我正在使用标记为 .html 的 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">
推荐阅读
- apache-kafka - 使用 curl 注册 Kafka Schema
- user-interface - 我一直在尝试将 GUI 类添加到菜单选项中,但菜单会随 GUI 打开
- python - 无法从 Dataframe 更新 Google 表格 - json 问题,单个单元格问题
- shopify - Shopify + CartJs 在添加/删除项目事件时运行运输栏更新功能
- supervisord - Supervisord - 达芙妮 - 没有名为项目的模块
- gradle - Gradle - Intellij 上的 MultipleCompilationErrorsException
- javascript - 在 vue 中将卡片堆叠在一起
- excel - 如何使用 VBA 将代码应用于文件夹中的多个文件?
- date - TypeError: 'value' 必须是 str 或 bytes 的实例,而不是浮点数,
- html - 设置网站的响应式背景图片(全宽和全高)