jquery - JQueryUI DatePicker 的问题
问题描述
我在使用 JQueryUI DatePicker 循环遍历具有类的一组元素时遇到问题,我似乎无法弄清楚我的问题出在哪里,因为控制台只是告诉我一个不应该未定义的元素是未定义的。
$(document).ready(function () {
$('.date').toArray().forEach(function (field) {
$(field).datepicker({
showOn: '',
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true,
yearRange: "c-100:c+100",
beforeShow: function () {
setTimeout(function () {
$(field).css('z-index', 999999);
}, 0);
}
});
let class_to_use = '.'+$(field).attr('id');
let icon = $(class_to_use);
console.log(icon); // fine, not undefined, it is the correct element
icon.click(function () {
console.log('clicked');
$(field).datepicker("show")
// but on click I get...
// Uncaught TypeError: Cannot read property 'left' of undefined
});
});
});
如果这很重要,这个输入是模态的,我做错了什么?
这是为其中一个元素呈现的 HTML,有多个元素,这就是我要循环的原因:
<i class="id_date fa fa-calendar-alt prefix prefix-smaller form-icon-adjust" style="cursor: pointer; color: var(--sidebar-bg-color)"></i>
<div class="md-form form-icon-margin ">
<input type="text" name="date" class="form-control date" required id="id_date">
解决方案
您可以简单地使用该button
选项并设置按钮样式来满足您的需求。您不必为自己的参与而战。
$(function() {
$('.date').each(function(i, el) {
var field = $(el);
$(field).datepicker({
showOn: 'button',
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true,
yearRange: "c-100:c+100"
});
var btn = field.next("button");
var icon = $("<i>", {
class: "fa fa-calendar-alt"
});
btn.addClass("id_date prefix prefix-smaller form-icon-adjust").css({
cursor: "pointer",
color: "var(--sidebar-bg-color)"
}).html(icon);
});
});
.date-control .id_date {
float: left;
width: 30px;
height: 32px;
margin: 1px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="md-form form-icon-margin date-control">
<input type="text" name="date" class="date" required id="id_date">
</div>
推荐阅读
- aframe - 第一次启动后如何在a-frame中循环播放下一个声音?
- django - 如何在不清除队列的情况下升级 Django Q 和 Django?
- mysql - csvsql导入mysql:使用Timestamp,需要Datetime
- ddl - 在 DELETE CASCADE 上不能在雪花中工作
- azure-cloud-services - IIS 10 失败的请求 500 - General_Request_Start 未能完成
- typo3 - 获取面包屑的tt_news记录的标题
- jenkins - Jenkins 管道 NoSuchMethodError (copyArtifacts)
- iis-7 - .Net Framework 身份服务器 3 - Windows 身份验证
- firebase - 在 Firestore 中更新或创建文档时避免重复 fieldPath
- ios - SwiftUI 中的导航协调器