开发者

jQuery attach same event to many id

开发者 https://www.devze.com 2023-02-05 14:34 出处:网络
My question is: How to attach the same series of events to many id\'s in this case I\'m attaching all of this code to \"#employeeBox\"

My question is: How to attach the same series of events to many id's

in this case I'm attaching all of this code to "#employeeBox"

$("#employeeBox").autocomplete({
                                source: function (request, response) {
                                    $.ajax({
                                        url: '<%:ResolveUrl("~/Lookup/SearchEnterpriseUsers")%>',
                                        dataType: "json",
                                        data: {
                                            featureClass: "P",
                                            style: "full",
                                            maxRows: 12,
                                            nameLike: request.term
                                        },
                                        success: function (data) {
                                            response($.map(data, function (item) {
                                                return {
                                                    label: item.FullName,
                                                    value: item.EmployeeId
                                                }
                                            }))
                                        }
                                    })
                                },
                                minLength: 2,
                                select: function (event, ui) {
                                    alert(ui.item.value)
                                },
                                open: function () {
                                    $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                                },
                                close: function () {
                                    开发者_开发问答$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                                }
                            });

What if I want to attach it to many id's.

Like

for (int i=0; i < count; i++)
"#employee_" + i + "_Box"

what's the best way to do this without a lot of repeated code?

thanks!


if you can, just add a class to all your elements and attach the event to that class instead of to a bunch of ids.

$('.myClass').live('myevent', function(e) { //some function });


If you gave them all a class of employeeBox you could just do $('.employeeBox').autocomplete().

0

精彩评论

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