简体   繁体   中英

First click is not working in jquery

I am trying to make a seatchart. All seats are elements. I click first seat and after that i click the other one. Second one changed its colour but first one is not working. Why?

function seatObject(id, label, status, token){
    this.id = id;
    this.label = label;
    this.status = status;
    this.token = token;
}

var seats = [];
var currentSeat;

function initAll() {
    $(".seatObj").each(function() {
        var id = $(this).attr("id");
        var temp = new seatObject("#" + id, "label" + id, "available", "");
        seats[id] = temp;        
        $("#" + id).click(function () {
            currentSeat = $(this).attr("id");
            if (seats[currentSeat].status == "selected")
            {
                dequeueSeat();
            }
            else
            {
                enqueueSeat();   
                //alert($(this).attr("inkscape:label"));                     
            }
        });
    }); 
}

No problem with your code dude...

You need little changes...

Ordinary Click does not attach an event handler functions for one or more events. So use on method - Attach an event handler function for one or more events to the selected elements.

$(".seatObj").on('click',"#" + id, function () {
        currentSeat = $(this).attr("id");
        if (seats[currentSeat].status == "selected")
        {
            dequeueSeat();
        }
        else
        {
            enqueueSeat();   
            //alert($(this).attr("inkscape:label"));                     
        }
});

You can use class instead of id in on method.

Try wrapping the function in $( document ).ready(function() {}

works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM