2

I want to use multiple datepickers in the same form and need altFields as well as certain logics for each of these datepickers. The datepickers are named dynamically such as date_1, date_2 etc. whereas the altFields are named date_1_alt, date_2_alt.

I don´t want to setup multiple definitions foreach datepicker, but just use one single definition by selector [id^='date_'] or i.e. by class .date

$("[id^='date_']").datepicker({
  dateFormat: "dd MM yy",
  altFormat:  "yy-mm-dd",
  altField: $(this).attr('id')+"_alt"
});

The problem is the dynamic definition of the altField (the code given does not work). I have tried many different ways but couldn't find the right way to define a dynamic assignment that would automatically create the corresponding altFields (such date_1 -> date_1_alt etc.)

Does anyone know the right way to get this done? The same issue is relevant to further logic, such as defining the onSelect functions etc.

Thanks for your help!

4

1 回答 1

0

您可以手动迭代每个匹配元素并以这种方式应用 datepicker:

$("[id^='date_']").each(function () {
    var $this = $(this);

    $this.datepicker({
        dateFormat: "dd MM yy",
        altFormat:  "yy-mm-dd",
        altField: $this.attr('id')+"_alt"
    });
});
于 2012-11-19T18:33:56.293 回答