开发者

Setting attributes to all datepickers and also individually

Suppose you have a page with multiple datepickers and you want to set some properties with the same value to all of them (with a class selector), and then set non shared properties to each one of them (with an id selector). How to do this?

Here is what I´ve tried so far

$(document).ready(function() {
    // Shared properties for all datepickers with class="datepicker"
    $( ".datepicker" ).datepicker({
        showOn: 'focus',
        showButtonPanel: true,
        showOtherMonths: true,
        selectOtherMonths: true,
        dateFormat: "d M yy"
    });

    // Specific properties for each datepicker
    $("#myDate1").datepicker({ 
      altFormat: "yy-mm-dd",
      altField: "#myDate1_alt"
    });

    $("#myDate2").datepicker({ 
      altFormat: "yy-mm-dd",
      altField: "#myDate2_alt"
    });
});

Markup

<input type='text' id='myDate1' class="datepicker"/>        
<input type="hidden"  id="myDate1_alt"/>        

<input type='text' id='myDate2' class="datepicker"/>        
<input type="hidden"  id="myDate2_alt"/>

It sets wel开发者_StackOverflow社区l the shared properties but not the specific ones. I also tried inverting the order ("#myDate1" - "#myDate2" - ".datepicker", but then the properties for ".datepicker" were gone).


You can pass the option verb to the datepicker() method:

// Specific properties for each datepicker.

$("#myDate1").datepicker("option", { 
    altFormat: "yy-mm-dd",
    altField: "#myDate1_alt"
});

$("#myDate2").datepicker("option", {
    altFormat: "yy-mm-dd",
    altField: "#myDate2_alt"
});


It should also be datePicker with a capital p:

$("#myDate1").datePicker({ 
    altFormat: "yy-mm-dd",
    altField: "#myDate1_alt"
});

When I changed to datePicker, your code worked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜