如何在 jquery ui 日期选取器中调整其他输入的大小
How to resize additional input in jquery ui Datepicker?
如何设置动态添加的input
字段的宽度,这些字段通过jQueryUI日期选择器小部件获取值?
我有 2 个输入,"日历输入"是我们选择日期的地方,它将通过altField
方法(日期选择器的核心方法)自动添加到我们的附加输入中。那么我可以动态调整input
的大小以使其更小''更大吗?
Additional input: <input type="text" id="alternate" value="December,19 2015"><br>
Calendar input: <input type="text" id="from">
$("#from").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
altField: "#alternate",
altFormat: "MM d, yy",
onClose: function (selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
}
});
这是小提琴的链接 - https://jsfiddle.net/mhf7kxo4/
这是您正在寻找的内容的工作小提琴。
基本输入自动调整大小插件(只是扩展了 isValidWidth更改此插件的限制):
(function($){
$.fn.autoResizeInput= function(o) {
o = $.extend({
maxWidth: 1000,
minWidth: 0,
comfortZone: 70
}, o);
this.filter('input:text').each(function(){
var minWidth = o.minWidth || $(this).width(),
val = '',
input = $(this),
testSubject = $('<tester/>').css({
position: 'absolute',
top: -9999,
left: -9999,
width: 'auto',
fontSize: input.css('fontSize'),
fontFamily: input.css('fontFamily'),
fontWeight: input.css('fontWeight'),
letterSpacing: input.css('letterSpacing'),
whiteSpace: 'nowrap'
}),
check = function() {
if (val === (val = input.val())) {return;}
// Enter new content into testSubject
var escaped = val.replace(/&/g, '&').replace(/'s/g,' ').replace(/</g, '<').replace(/>/g, '>');
testSubject.html(escaped);
// Calculate new width + whether to change
var testerWidth = testSubject.width(),
newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth,
currentWidth = input.width(),
isValidWidthChange = ( (newWidth < currentWidth || newWidth > currentWidth )&& newWidth >= minWidth)
|| (newWidth > minWidth && newWidth < o.maxWidth);
// Animate width
if (isValidWidthChange) {
input.width(newWidth);
}
};
testSubject.insertAfter(input);
$(this).bind('keyup keydown blur update', check);
});
return this;
};
})(jQuery);
只需添加以下行:
$("#from")
.datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
altField: "#alternate",
altFormat: "MM d, yy",
onClose: function (selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
$("#alternate").focus().blur();
}
});
$("#alternate")
.autoResizeInput({
maxWidth: 18,
minWidth: 11,
comfortZone: 5
})
.focus()
.blur();
由于您只想在日期选择器日期选择上做一些事情,因此您可以将内容添加到日期选择器的onClose
或onSelect
回调中
例:
$("#from").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
altField: "#alternate",
altFormat: "MM d, yy",
onClose: function (selectedDate) {
// do you stuff over here
$("#alternate").width(100).removeClass("disabled");
$("#to").datepicker("option", "minDate", selectedDate);
}
});
你的意思是这样吗?
$("#from").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
altField: "#alternate",
altFormat: "MM d, yy",
onClose: function (selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
$('input').css('width','auto');
}
});
jsFiddle 演示
如果您不想使用插件,您可能希望看到以下解决方案:
将输入字段的宽度调整为其输入
相关文章:
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 如何添加浮动和非浮动,其他
- 调整窗口大小时,可拖动的对象会出现在容器外部
- 如何使用可调整大小的元素重叠其他元素
- 根据其他调整大小的html元素定位背景图像
- 调整从其他域加载的iframe/object标记的内容大小
- 如何调整从其他用户加载的图像的大小,以便保持尺寸,同时使用 css 设置最大宽度和高度?(HTML/JS/CSS)
- 如何避免由其他事件触发的多次窗口大小调整
- 如何在 jquery ui 日期选取器中调整其他输入的大小
- 如何在输入字段 #1 中键入时自动调整其他输入字段的大小
- 获取将图像调整为其容器大小的脚本的名称+其他功能
- 从其他域加载内容后,可以调整iFrame的大小
- HTML图像&其他组件根据页面宽度调整大小
- jQuery JavaScript使页脚或其他元素在页面调整大小时消失
- 调整iframe的大小以适应其他页面
- 如何调整这个jquery键盘导航以允许其他标签和键盘功能
- 打开文件夹中的所有pdf文件,并保存为调整大小的jpg在其他文件夹
- 如何调整空格键和回车只适用于第三个按钮,不适用于任何其他按钮
- 物品高度随其他物品调整
- 根据不同分区中的其他表头高度调整表头高度