Javascript 使用 jQuery 获取列表项的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5548827/
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-08-23 17:44:46 来源:igfitidea点击:
Get value of List Item with jQuery
提问by user692415
How to get value and index of list item onClick event with jQuery?
for example:
如何使用jQuery获取列表项onClick事件的值和索引?
例如:
<ul id='uItem'>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
回答by Aron Rotteveel
回答by Hussein
$('#uItem li').click(function(){
var $this = $(this);
alert('Text ' + $this.text() + 'Index ' + $this.index());
})
Check working example at http://jsfiddle.net/yccyJ/1/
在http://jsfiddle.net/yccyJ/1/检查工作示例
回答by user2037235
If you had set a value attribute for your li:
如果您为 li 设置了 value 属性:
<ul id='uItem'>
<li value="item1">Item 1</li>
<li value="item2">Item 2</li>
<li value="item3">Item 3</li>
<li value="item4">Item 4</li>
</ul>
, then you can retrieve it using jQuery like this:
,然后您可以像这样使用 jQuery 检索它:
$('#uItem li').click(function(){
var $this = $(this);
var selKeyVal = $this.attr("value");
alert('Text ' + $this.text() + 'value ' + selKeyVal);
})
回答by Andrei Andrushkevich
回答by Alex
Have a look at the index function, http://api.jquery.com/index/
看看索引函数,http://api.jquery.com/index/