我使用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");
}
});
有线索吗?
发布于 2020-01-20 21:07:34
您可以将旧值持久化在缓存中,可以使用.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">
发布于 2020-01-20 21:08:11
$('#date').on('focusin', function() {
$(this).data('val', $(this).val());
});
$('#date').change(function(event) {
var prev = $(this).data('val');
var current = $(this).val();
console.log("Prev value " + prev);
console.log("New value " + current);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="date" />
https://stackoverflow.com/questions/59834351
复制相似问题