简体   繁体   English

如何检查DOM中是否已经存在三个元素

[英]How can I check if three elements already exist together in the DOM

I've got a trick one that I just can't solve. 我有一个我无法解决的窍门。 I have a form that consists of address, day and time. 我有一个包含地址,日期和时间的表格。

I need to check if the day and time have already been assigned to another address. 我需要检查日期和时间是否已经分配给另一个地址。 And, if so, it doesn't add it to the table. 而且,如果是这样,则不会将其添加到表中。

I've been thinking about this one. 我一直在想这个。 But I just can't figure it out. 但我只是想不通。

Code Follows: 代码如下:

$('#diahora').click(function(event){ 
        //...var declarations...//

    var novoTotal = $("<tr>").append("<th class='endereco-usado'>" + addEnd + "<th class='dia-usado'>" + novoDia + "<th class='hora-usada'>" + novaHora).append("<th>" + "<a class='btn-xs btn-danger btn delHora'>Deletar</a>").append("<input class='horashidden flex-direction-nav'>").val(addEnd + ' ' + novoDia + ' ' + novaHora);

    var outroTotal = $("<tr>").append("<th>" + addEnd + "<th>" + novoDia + "<th>" + novoOutros1 + ' - ' + novoOutros2).append("<th>" + "<a class='btn-xs btn-danger btn delHora'>Deletar</a>").append("<input class='horashidden flex-direction-nav'>").val(addEnd + ' ' + novoDia + ' ' + novoOutros1 + ' - ' + novoOutros2);

    if(horaSelecao.val()== 'outros'){
        $('.diasehoras tbody').append(outroTotal);
    }else if($('.diasehoras tbody').text().indexOf(novoTotal.text()) !== -1){
        return alert('Dia e hora já selecionados.');
    }else{
        $('.diasehoras tbody').append(novoTotal);
    }
});

So, I already have a system that identifies if the complete set of the address exists in the DOM. 因此,我已经有了一个可以识别DOM中是否存在完整地址集的系统。 But now, I need to check that above statement.... 但是现在,我需要检查以上声明。

What is the best to deal with this? 最好的办法是什么?

you can store them in a map of some sort (i think this will work but i'm on my phone and it's been a long day): 您可以将它们存储在某种形式的地图中(我认为这可以工作,但是我在用手机,已经过了很长的一天):

var map = {};

if (map.hasOwnProperty(some_id)) {
    // already in the map
} else { 
    // added it to the map as a key/value pair
    // alternately use 'dot' notation, i.e., map.some_id = value
    map["some_id"] = your_unique_string_here;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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