为什么Jquery UI会擦除我的输入框
Why does Jquery UI erase my input box?
我正在做一个.net MVC应用程序,我用小提琴重新创建了这个问题。
当我试图更改我的日期时,日历会停留在那里,当我点击屏幕上的其他地方时,日历就会消失,但其他日期会被删除。
我只想在选择日期时日历消失,而不是删除其他日期
$('#from').datepicker({
inline: true,
showOtherMonths: true,
dateFormat: 'dd.mm.yy.',
firstDay: 1,
changeMonth: true,
changeYear: true,
yearRange: "-50:+50",
onClose: function (selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
}
});
$('#to').datepicker({
inline: true,
showOtherMonths: true,
dateFormat: 'dd.mm.yy.',
firstDay: 1,
changeMonth: true,
changeYear: true,
yearRange: "-50:+50",
onClose: function (selectedDate) {
$("#from").datepicker("option", "maxDate", selectedDate);
}
});
$(".dates").change(function () {
var dateFrom = $("#from").val();
//check that there is data before saving into sessionStorage
if (dateFrom.value.length >= 8)
sessionStorage.setItem("fromDate", this.value)
var dateTo = $("#to").val();
//check that there is data before saving into sessionStorage
if (dateTo.value.length >= 8)
sessionStorage.setItem("toDate", dateTo)
if (dateTo != null && dateFrom !=null) {
$.ajax({
url: '/MainPage/DateSessionCreate',
type: 'POST',
data: { "dateTo": dateTo, "dateFrom": dateFrom },
dataType: 'json'
});
}
else {
alert("one of the dates is null");
}
});
var today = new Date();
var curr_date = today.getDate();
var curr_month = today.getMonth();
curr_month++;
var curr_year = today.getFullYear();
var toDate = "";
//check if there's a to Date in sessionStorage
if (sessionStorage.getItem("toDate")) {
toDate = sessionStorage.getItem("toDate");
sessionStorage.setItem("toDate", toDate);
} else {
toDate = curr_date + "." + curr_month + "." + curr_year;
sessionStorage.setItem("toDate", toDate);
if (toDate != null) {
$.ajax({
url: '/MainPage/DateSessionCreate',
type: 'POST',
data: { "dateTo": toDate},
dataType: 'json'
});
}
}
document.getElementById("to").value = toDate;
today.setDate(today.getDate() - 7);
var new_date = today.getDate();
var new_month = today.getMonth();
new_month++;
var new_year = today.getFullYear();
var fromDate = "";
//check if there's a from Date in sessionStorage
if (sessionStorage.getItem("fromDate")) {
console.log("if");
fromDate = sessionStorage.getItem("fromDate");
sessionStorage.setItem("fromDate", fromDate);
} else {
console.log("else");
fromDate = new_date + "." + new_month + "." + new_year;
sessionStorage.setItem("fromDate", fromDate);
if (fromDate != null) {
$.ajax({
url: '/MainPage/DateSessionCreate',
type: 'POST',
data: { "dateFrom": fromDate },
dataType: 'json'
});
}
}
document.getElementById("from").value = fromDate;
我承认,我还是个新手,如果我知道如何让日期选择器在输入框中写下日期,整个代码本可以缩短(但当我使用"defaultDate:-7"时,它只会突出显示日历中的日期)。这就是我创建日期格式并手动将其写入输入框的原因。附加代码是为了确保页面重新加载后日期保持不变,有些代码是为了向控制器发送信息
EDIT1有人指出onClose函数混淆了。虽然它确实解决了擦除问题,但它带来了另一个问题——我故意放这些来限制日期范围。这样,"到"日期不能在"开始"日期之前。也许还有其他方法可以限制这种情况?
您可以更改日期的格式,使其最终不包含.
:
dateFormat: 'dd.mm.yy',
此外,$("#from").val()
的返回值是字符串,因此它没有value属性。您可以检查字符串的长度:
if (dateFrom.length >= 8)
检查这个小提琴
要设置日期选择器的日期,可以使用setDate
方法:
var today = new Date();
$('#to').datepicker("setDate", today);
$('#from').datepicker("setDate", new Date(today.setDate(today.getDate() - 7)));
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 我如何在数字插入中使用逗号,这样它就不会'不要破坏我的输入字段
- 为什么Jquery UI会擦除我的输入框
- 为什么获胜't我的输入值总和
- jQuery:在验证错误之后,我的输入掩码函数不会'Don’不要在野外工作
- 我的输入值'对话框窗体上的s;t更新
- 我的输入字段听到除回车之外的每个“键下”事件.我有一个日期选择器,不确定发生了什么
- 为什么我的输入接受所有值,即使我正在使用函数来防止这种情况
- IE不喜欢我的输入和图像标签
- 谷歌地图(它没有将结果显示到我的输入窗格中以保存到我的数据库中)
- 编码然后在 base64 中解码不会输出我的输入数组缓冲区
- 为什么我的 if 语句仅在我的输入具有值时才有效
- 为什么我的输入框的值没有返回
- jQuery/CoffeeScript/Rails 不会在我的输入上调用 change()
- Jquery - .prop 不会动态选中我的输入复选框
- 我想在搜索完成后保留我的输入字段数据
- 为什么我的输入被插入到具有相同索引的两个diff数组中?(js)
- 我的输入类型=“文本”未正确验证我的电子邮件地址
- 我的数据绑定有效,但我的输入ng模型是't更新时更新
- 为什么我的输入从未被隐藏