开发者

assign multiple ids to an event

开发者 https://www.devze.com 2023-01-20 02:26 出处:网络
I have a form where I\'ve got multiple text inputs for various timefields, each with a unique ID set for some other code that I\'m using. I\'m also trying to use a time entry script that has a simple

I have a form where I've got multiple text inputs for various timefields, each with a unique ID set for some other code that I'm using. I'm also trying to use a time entry script that has a simple single line bit of code to implement, but as I have 28 different fields all wit开发者_JAVA技巧h different IDs, this is going to get repetitve fast. Is there a way to reference in the jquery code to reference the same function across multiple IDs without duplicating the typing?

example:

html

<input id="M_start_time1" />
<input id="M_end_time1" />
<input id="M_start_time2" />
<input id="M_end_time2" />

jquery

$('#M_start_time1').timeEntry({
    ampmPrefix: ' ',
});
$('#M_end_time1').timeEntry({
    ampmPrefix: ' ',
});
$('#M_start_time2').timeEntry({
    ampmPrefix: ' ',
});
$('#M_end_time2').timeEntry({
    ampmPrefix: ' ',
});

Any suggestions are greatly appreciated! :)


If you still want to assign multiple id's to an event you can use this :

$('#M_start_time1, #M_end_time1, #M_start_time2, #M_end_time2').timeEntry({
    ampmPrefix: ' ',
});


Give your input fields a common class:

<input id="M_start_time1" class="something" />
<input id="M_end_time1" class="something" />

and use a single class selector to do all the work:

$('.something').timeEntry({
    ampmPrefix: ' ',
});


You could use the jQuery starts with attribute selector if all your ids of interest start with something, and none of the ids of inputs you're not interested in start with that:

$('input[id^=M_]').timeEntry({
    ampmPrefix: ' '
});

Try out the starts with attribute selector with this jsFiddle

0

精彩评论

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