javascript 来自列表项的 jquery id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5221173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:25:06  来源:igfitidea点击:

jquery id from list items?

javascriptjquerymysqllist

提问by Henrikz

Ok, so i've got a list of usernames from the mysql database. But, i need the ID from the database so i was thinking something like this,

好的,所以我从 mysql 数据库中获得了用户名列表。但是,我需要数据库中的 ID,所以我在想这样的事情,

<li id='43'>Monica</li>
<li id='47'>Henrik</li>
<li id='77'>Eric</li>

But how can i get the ID from the list?

但是如何从列表中获取 ID?

回答by roryf

I would use something like the following:

我会使用类似以下内容:

<ul id="items-list">
    <li id='member-43'>Monica</li>
    <li id='member-47'>Henrik</li>
    <li id='member-77'>Eric</li>
</ul>

then:

然后:

$('#items-list li').each(function() {
    var id = $(this).attr('id').split('-')[1];
});

It was pointed out in the comments that as of HTML5it's perfectly valid to start an idattribute with a number, but I'd still give preference to this method, especially if there are to be multiple lists on a page from different DB tables (ids still need to be unique). An idof 43has no meaning to anyone, but member-43is much more clear.

评论中指出,从 HTML5开始id,以数字开头的属性是完全有效的,但我仍然优先使用这种方法,特别是如果页面上有来自不同数据库表的多个列表(ids仍然需要是唯一的)。一个idof43对任何人都没有意义,但member-43要清楚得多。

回答by Ashik

Have a normal javascript on click event for the list element as shown below:

有一个普通的 javascript 点击事件的列表元素,如下所示:

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
      linkClicked = function (index) {
        alert($('#myList li').get(index).id);
      }
    </script>
  </head>
  <body>
    <ul id='myList'>
      <li id='43' onclick="linkClicked($(this).index())">Monica</li>
      <li id='47' onclick="linkClicked($(this).index())">Henrik</li>
      <li id='77' onclick="linkClicked($(this).index())">Eric</li>
    </ul>
  </body>
</html>

This worked for me!

这对我有用!

回答by Re3L

You could add a class with the same name to all of the list items

您可以向所有列表项添加同名的类

<li class="test" id='43'>Monica</li>
<li class="test" id='47'>Henrik</li>
<li class="test" id='77'>Eric</li>

Then you could use an on

然后你可以使用一个

$(".test").click(function(){
$(this).attr("id");
});

回答by Heemanshu Bhalla

You Can Use HTML5 Data Attribute in order to hold ID Data . Holding data in ID may give problem if you require to define id statically at some place so not an optimal solution You can check code below -

您可以使用 HTML5 数据属性来保存 ID 数据。如果您需要在某个地方静态定义 id 因此不是最佳解决方案,则将数据保存在 ID 中可能会出现问题您可以查看下面的代码 -

<li  data-empid="A123" data-salary="120" class="datalist">Monica</li>
<li data-empid="A124" data-salary="121" class="datalist">Monica</li>

Access Data in Data Attribute in Jquery

在 Jquery 中访问数据属性中的数据

<script type="text/javascript">
 $( "li .datalist" ).click(function() {    
       $(this).data("salary")
    });
</script>

回答by hsz

$('li').each(function(){
  alert( $(this).attr('id') );
});

回答by Nikhil

Try

尝试

$("li").eq(0).attr("id")

Substitute 0 by the index item of the list item you want to read the id of.

将 0 替换为要读取其 id 的列表项的索引项。

回答by Ivan Ivani?

Do you actually want to do something with that id? There is too little data that you gave us. If you want to administer users you would need something like this:

你真的想用那个 id 做点什么吗?你给我们的数据太少了。如果你想管理用户,你需要这样的东西:

<ul id="deleteUsers">
  <li id='user43'><a href="deleteuser.php?id=43">Monica</a></li>
  <li id='user47'><a href="deleteuser.php?id=47">Henrik</a></li>
  <li id='user77'><a href="deleteuser.php?id=77">Eric</a></li>
</ul>

And than in jQuery override click event. roryf gave you good example.

并且比在 jQuery 中覆盖单击事件。roryf 给了你很好的例子。