开发者

Is it possible to get the same datepicker by different selectors (jquery)

this might be a basic question about jquery, but I hope someone will clarify for me anyway.

I have created two datepickers on my page (pseudo-code):

<input id="datepicker1" class="datepicker"/>
<input id="datepicker2" class="datepicker" />

jQuery(".datepicker").datepicker();

if I do jQuery("#datepicker1") will I then get the same datepicker-instance whic开发者_如何学Goh was created using the class-selector?

(I have tried the above because I need to add "onSelect" to datepicker1, but it does not work.)

thanks Thomas


if I do jQuery("#datepicker1") will I then get the same datepicker-instance which was created using the class-selector?

Sure thing (as @BoltClock points out, you will retrieve the same DOM element that the datepicker was applied to). Here's how you could define an event handler for just one of those datepicker inputs:

jQuery("#datepicker1").datepicker("option", "onSelect", function() {
    alert('select');
});

The event handler will only be fired for #datepicker1.

Example: http://jsfiddle.net/andrewwhitaker/TsHwa/


Here's an alternate way of only executing code in onSelect for the input with id datepicker1:

jQuery(".datepicker").datepicker({
    onSelect: function () {
        if (this.id === 'datepicker1') {
            alert('select');
        }
    }
});

Example: http://jsfiddle.net/andrewwhitaker/rdxy6/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜