formwatchfieldCode date_of_join data = if datadate_of_join && datadate_of_joinlength var currentDate = new Date; var oneMonthEarlier = new DatecurrentDategetFullYear currentDategetMonth - 1 current
<p>要让上述JavaScript代码可以执行,你需要将其放置在HTML文件中的<script>标签中。例如:</p>
<pre><code class="language-html"><!DOCTYPE html>
<html>
<head>
<title>My Form</title>
</head>
<body>
<form>
<input type="date" name="date_of_join">
<button type="submit">Submit</button>
</form>
<script>
function watchForm() {
var form = document.querySelector('form');
form.watch([{
'fieldCode': 'date_of_join'
}], function(data) {
if (data['date_of_join'] && data['date_of_join'].length) {
var currentDate = new Date();
var oneMonthEarlier = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, currentDate.getDate());
var minDate = oneMonthEarlier.toISOString().split('T')[0];
var selectedDate = new Date(data['date_of_join']);
var selectedTimestamp = selectedDate.getTime();
console.log("selectedTimestamp=", selectedTimestamp)
var oneMonthEarlierTimestamp = oneMonthEarlier.getTime();
console.log("oneMonthEarlierTimestamp", oneMonthEarlierTimestamp)
if (selectedTimestamp < oneMonthEarlierTimestamp) {
form.actions({
type: 'msgbox',
message: "最早日期不得早于" + minDate,
data: { 'date_of_join': '' }
});
// alert("最早日期不得早于" + minDate);
}
}
});
}
watchForm();
</script>
</body>
</html>
</code></pre>
<p>在上面的示例中,我们将代码包裹在<code>watchForm</code>函数中,并在页面加载完成后调用该函数。这样,当表单中的日期字段发生变化时,将触发<code>watch</code>方法,并执行相应的逻辑</p>
原文地址: https://www.cveoy.top/t/topic/iKtV 著作权归作者所有。请勿转载和采集!