jQuery Datepicker-为所有日期选择器自动定义altField [英] jQuery Datepicker - Automatically define altField for all datepickers

查看:156
本文介绍了jQuery Datepicker-为所有日期选择器自动定义altField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有的日期选择器都有一个自动生成的隐藏字段,该字段具有与日期选择器输入相同的ID,但前面加了下划线.

All my datepickers have an automatically-generated hidden field which has the same ID as the datepicker input, but with an underscore prepended.

<div class="datepicker">
    <input id="MyField1" type="text" value="" />
    <input id="_MyField1" type="hidden" value="" />
</div>

<div class="datepicker">
    <input id="MyField2" type="text" value="" />
    <input id="_MyField2" type="hidden" value="" />
</div>

然后将所有具有类datepicker的字段都设置为日期选择器.

And then any field which has class datepicker gets made into a datepicker.

$('.datepicker input').datepicker({
    altFormat: 'yy-mm-dd',
    changeMonth: true,
    constrainInput: true,
    dateFormat: 'dd/mm/y',
    minDate: '+0d',
    numberOfMonths: 2
});

但是我又如何自动获取它来为每个对象设置altField选项呢?

But how can I also automatically get it to set the altField option for each one?

这不起作用.我还尝试了在原始选项中进行分配,但这也不起作用.

This doesn't work. I also tried doing the assignment in the original options but that doesn't work either.

$('.datepicker input').each(function() {
    var altField = '_' + $(this).prop('id');
    $(this).datepicker('option', 'altField', altField);
});

推荐答案

altField 选项接受一个选择器,一个jQuery对象或一个元素,而不是id属性.

The altField option takes a selector, a jQuery object or an element, not an id attribute.

尝试指定 id选择器:

$(this).datepicker("option", "altField", "#_" + $(this).prop("id"));

或者,保存对$()的呼叫和对prop()的呼叫:

Or alternatively, saving a call to $() and a call to prop():

$(this).datepicker("option", "altField", "#_" + this.id);

这篇关于jQuery Datepicker-为所有日期选择器自动定义altField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆