jQuery-如何查找具有特定ID的元素? [英] jQuery - how can I find the element with a certain id?
问题描述
我有一张桌子,它的每个td
都有一个唯一ID ,它对应于一些时间间隔(0800
til 0830
... 0830
til 0900
和很快).
I have a table and each of its td
has a unique id that corresponds to some time intervals (0800
til 0830
... 0830
til 0900
and so on).
我有一个输入文本,用户可以在其中输入要阻止的时间间隔.
I have an input text where the user will type the time intervals they want to block.
如果他们键入了我的表中不存在的间隔,换句话说,如果他们键入了与我的td's id's
中的任何一个都不对应的间隔,我想显示一条警报,提示诸如
If they type an interval that doesn't exist in my table, in other words, if they type an interval that doesn't correspond to any of my td's id's
, I want to show an alert saying something like this interval is not available for blocking
.
但是,我很难找到此ID.
But, I'm having difficulty to find this id.
我正在这样做:
var horaInicial = $("#horaInicial").val().split(':')[0] + $("#horaInicial").val().split(':')[1]; // this is remover the ":" from a formatted hour
var verificaHorario = $("#tbIntervalos").find("td").attr("id", horaInicial);
但是verificaHorario
实际上是将我所有的td设置为此horaInicial ID.
But this verificaHorario
is actually setting all my td's to this horaInicial id.
如何找到表中的ID,如果不存在该ID则显示警报?
How can I find the id in my table and if it doesn't exist show some alert?
推荐答案
如果您尝试通过id查找元素,则无需仅搜索表-它在页面上应该是唯一的,因此您应该可以使用:
If you're trying to find an element by id, you don't need to search the table only - it should be unique on the page, and so you should be able to use:
var verificaHorario = $('#' + horaInicial);
如果出于某种原因只需要在表中搜索,则可以使用:
If you need to search only in the table for whatever reason, you can use:
var verificaHorario = $("#tbIntervalos").find("td#" + horaInicial)
这篇关于jQuery-如何查找具有特定ID的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!