在点击功能

时间:2016-02-24 09:24:27

标签: jquery

嗨,如果我将console.log移动到第一次单击它显示一个日志,则单击第二个元素时,我有2个单击功能,控制台显示重复ID。如何解决这个问题。我想点击2模态并返回没有重复的“id”。谢谢你

function clickForDelete(){
        $('.information').click(function(event){               
            var id = this.id;
            $('#deleteItem').click(function(event){
                console.log(id);
                deleteItem(id);
            });
            $('#checkInfo').click(function(event){
                info(id);
            });
        });
    }

First image

Second image

2 个答案:

答案 0 :(得分:1)

它被称为“冒泡”,它可以通过上述功能触发事件。所以你可以用stopPropagation()

来阻止它
function clickForDelete(){
    $('.information').click(function(event){               
        var id = this.id;
        $('#deleteItem').click(function(event){
            console.log(id);
            deleteItem(id);
            event.stopPropagation(); // see here;
        });
        $('#checkInfo').click(function(event){
            info(id);
            event.stopPropagation(); // and here;
        });
    });
}

答案 1 :(得分:0)

这是旧代码。我目前的项目是我使用json,但它在办公室的电脑里



$(function () {
                $('#datetimepicker1').datetimepicker({
                    format: 'YYYY-MM-DD',
                    daysOfWeekDisabled: [-1, 8],
                    showTodayButton: true,
                });

                //Select current time in DB
                   addDate = $('#datetxt').val();
                   checkDate(addDate);
                function checkDate(addDate){
                   if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                    } else {
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange = function() {
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        var bgColor;
                        var M = xmlhttp.responseText;
                        //console.log(M);
                        if(M == ""){
                            //console.log("เข้าเคลีย");
                            clearTable();
                        }
                        var departmentName = M.split(" ");
                        console.log(departmentName);
                        //console.log(departmentName);
                        for(var i = 0; i<departmentName.length;i++){
                            if(departmentName[roomNumber] != undefined && departmentName[roomNumber] != "" || departmentName[departName] != undefined && departmentName[departName] != "" || departmentName[time] != undefined && departmentName[time] != "" || departmentName[thead] != undefined && departmentName[thead] != "" || 
                                departmentName[amPeople] != undefined && departmentName[amPeople] != "" || departmentName[baverage] != undefined && departmentName[baverage] != "" ||
                                departmentName[foodNum] != undefined && departmentName[foodNum] != "" || departmentName[mealNum] != undefined && departmentName[mealNum] != ""){
                                var slr = selectRoom(departmentName[roomNumber]);                               
                                var fcr = fillColorInRoom(departmentName[departName]);                               
                                var cti = changeTimeToInt(departmentName[time]);          
                                //console.log(departmentName[checkDates]);            
                                selectValueFromDB(slr,fcr,cti,departmentName[departName],departmentName[time],departmentName[thead],departmentName[amPeople],departmentName[baverage],departmentName[foodNum],departmentName[mealNum]);
                                roomNumber = roomNumber + 10;
                                departName = departName + 10;                               
                                time = time +10;
                                thead = thead +10;
                                amPeople = amPeople +10;
                                baverage = baverage +10;
                                foodNum = foodNum +10;
                                mealNum = mealNum +10;
                                checkDates = checkDates +10;
                            }
                        }
                        }
                    }
                    xmlhttp.open("GET","select.php?q="+addDate,true);
                    xmlhttp.send();
                    }
  
  
&#13;
&#13;
&#13;