简体   繁体   English

使用jQuery UI datepicker将输入字段数组填充为备用字段

[英]Populating array of input fields as alternate fields using jQuery UI datepicker

I am using jquery ui datepicker in order to populate start and end dates for multiple events on the same page, the number of events is dynamic and I need to post day, month and year as separate fields, so I have something like this: 我正在使用jquery ui datepicker来填充同一页面上多个事件的开始和结束日期,事件的数量是动态的,我需要将天,月和年发布为单独的字段,所以我有以下内容:

<input type="text" name="event[0].startDate" class="date"/>
<input type="hidden" name="event[0].startDate_day" class="startDate_day" />
<input type="hidden" name="event[0].startDate_month" class="startDate_month" />
<input type="hidden" name="event[0].startDate_year" class="startDate_year" />

<input type="text" name="event[0].endDate" class="date"/>
<input type="hidden" name="event[0].endDate_day" class="endDate_day" />
<input type="hidden" name="event[0].endDate_month" class="endDate_month" />
<input type="hidden" name="event[0].endDate_year" class="startDate_year" />

event[1]...
event[2]...

I started working on jQuery functionality and this is what I have so far, which will populate alternate fields for startDate by class, of course it will not do what I need because I need to populate by field name rather then class: 我开始研究jQuery功能,到目前为止,这是我所拥有的,它将按类填充startDate的备用字段,当然,它不能满足我的需要,因为我需要按字段名而不是类进行填充:

$(".date").datepicker({
    onClose: function(dateText,picker) {
        $('.startDate_month').val( dateText.split(/\//)[0] );
        $('.startDate_day').val( dateText.split(/\//)[1] );
        $('.startDate_year').val( dateText.split(/\//)[2] );
}});

I need help figuring out how can I get the name of the input field within datepicker function so the alternate field assignment done by that field name + _day, month, year so this function can work for all the events on the page, making function above look more like: 我需要帮助弄清楚如何在datepicker函数中获取输入字段的名称,以便由该字段名称+ _day,month,year来完成备用字段分配,以便该函数可用于页面上的所有事件,从而使该函数生效看起来更像:

$(".date").datepicker({
    onClose: function(dateText,picker) {
        $('input[' + $name + '_month' + ']').val( dateText.split(/\//)[0] );
        $('input[' + $name + '_day' + ']').val( dateText.split(/\//)[1] );
        $('input[' + $name + '_year' + ']').val( dateText.split(/\//)[2] );
}});

Hope that makes sense :) Thanks 希望有道理:)谢谢

Two possibilities. 两种可能性。

One is to give each item an ID as well (eg <input id="event_0_endDate_day"… , which is incidentally how Rails handles the naming conversion), and then select it with 一种方法是为每个项目都指定一个ID(例如<input id="event_0_endDate_day"… ,这也是Rails处理命名转换的方式),然后使用

$('#event_' + index + '_endDate_day')

The other is to use the property selection bits of Sparkle: 另一种是使用Sparkle的属性选择位:

$('input["name"="event[' + index + '].startDate_day"])

The former would be greatly preferred, as the latter will perform much, much more poorly, but perhaps you have no other choice. 前者将是更可取的选择,因为后者的性能要差得多,但也许您别无选择。

edit : 编辑

More specifically: 进一步来说:

<input type="text" id="event_0_startDate" name="event[0].startDate" class="date"/>
<input type="hidden" id="event_0_startDate_day" name="event[0].startDate_day" class="startDate_day" />
<input type="hidden" id="event_0_startDate_month" name="event[0].startDate_month" class="startDate_month" />
<input type="hidden" id="event_0_startDate_year" name="event[0].startDate_year" class="startDate_year" />

<input type="text" id="event_0_endDate" name="event[0].endDate" class="date"/>
<input type="hidden" id="event_0_endDate_day" name="event[0].endDate_day" class="endDate_day" />
<input type="hidden" id="event_0_endDate_month" name="event[0].endDate_month" class="endDate_month" />
<input type="hidden" id="event_0_endDate_year" name="event[0].endDate_year" class="startDate_year" />

Then, you can do: 然后,您可以执行以下操作:

$(".date").datepicker({
    onClose: function(dateText,picker) {
        var prefix = $(this).attr('id');
        $('#' + prefix + '_month').val( dateText.split(/\//)[0] );
        $('#' + prefix + '_day').val( dateText.split(/\//)[1] );
        $('#' + prefix + '_year').val( dateText.split(/\//)[2] );
    }
});

To to add my comment to this problem. 要添加我对这个问题的评论。 I used the id="" to assign a counter onto every field so that I could successfully add a jQuery Date picker to each field 我使用id =“”在每个字段上分配了一个计数器,这样我就可以成功地向每个字段添加jQuery日期选择器

Html HTML

<input class="span2" onmouseover="autoCompleteMedicalHistory();"  type="text" id="MedicalHistoryOnset0" name="MedicalHistoryOnset[]" placeholder="Date of Diagnosis"/>

Javascript Java脚本

var counter=1
var row=' <input class="span2" onmouseover="autoCompleteMedicalHistory();"  type="text" id="MedicalHistoryOnset'+counter+'" name="MedicalHistoryOnset[]" placeholder="Date of Diagnosis"/>"

Jquery Date Picker jQuery日期选择器

jQuery("#MedicalHistoryOnset"+counter).datepicker({dateFormat : 'yy-mm-dd'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM