gpt4 book ai didi

javascript - 获取 jQuery 中可选项目的标题

转载 作者:行者123 更新时间:2023-11-29 21:46:55 25 4
gpt4 key购买 nike

我有一个可选按钮列表,其名称是通过读取 JSON 文件动态生成的。每次单击按钮时,我都希望获得它的标题,即 row["name"]。这是我的相关代码和 JSON:

<head>


<script>

$.getJSON("json-data.txt",function(data){
var items = [];
$.each(data['data'],function(i, row){
items.push("<li class = 'ui-widget-content'>" + row["name"] + "</li>");
});
$("<ol/>",{
"id" : "selectable",
html : items.join("")
}).appendTo("body");
});

var selectableObj = {
selected: function(event, ui){

}
}

$(function() {
$( "#selectable" ).selectable(selectableObj);
});
</script>

</head>
<body>

</body>

JSON 数据:

{
"data": [
{
"name": "ABC",
"visited" : "Yes"
},
{
"name": "DEF",
"visited" : "Yes"
},
{
"name": "GHI",
"locked": "No"
},
],

}

这是有效的,因为我得到了格式为的可选列表:

<ol id="selectable">
<li class="ui-widget-content">ABC</li>
<li class="ui-widget-content">DEF</li>
<li class="ui-widget-content">GHI</li>
</ol>

当我点击第一个按钮时,我想获得值“ABC”。我不知道该怎么做。我阅读了有关 .text() 的信息,但无法理解如何使用它。有人可以帮忙吗?

编辑 - 根据一些评论,我像这样更改了我的代码,但它不起作用:

<script>

$(function() {
$( "#selectable" ).selectable();
});

$.getJSON("json-data.txt",function(data){
var items = [];
$.each(data['data'],function(i, row){
items.push("<li class = 'ui-widget-content'>" + row["name"] + "</li>");
});
$("<ol/>",{
"id" : "selectable",
html : items.join("")
}).appendTo("body");
});


$('.ui-widget-content').click(function(){
var text = $(this).text(); // this get the text
alert(text); // do whatever you want with it
});
</script>

最佳答案

我想这就是你要找的

selected: function() {
$( ".ui-selected", this ).each(function() {
alert($(this).text());
});
}
});

DEMO

关于javascript - 获取 jQuery 中可选项目的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com