简体   繁体   English

如何从无序列表中选择项目

[英]How to select item from unordered list jquery

I have a function that fills my unordered list. 我有一个函数可以填充我的无序列表。

$("#DBSearch").append('<ul id="List" data-role="listview">');


for(var i = 0; i < 10; i++)
{           
    $("#DBSearch").append("<li value=" + i + "><a href="+ "#" + ">" + obj.Search[i].Title + "</a></li>" );      
}

When I click on an item in the list I want my text box strSearch to fill with that item. 当我单击列表中的一个项目时,我希望我的文本框strSearch填充该项目。

When you click a li in list dbsearch it will replace the text with strSearch 单击列表dbsearch中的li ,它将用strSearch替换文本

$("#List").on("click", "li", function() {
    $("#strSearch").val($(this).text());
});

If you want to replace the text of the nested a link use $("a", this).text(...) . 如果您想更换嵌套的文字a链接使用$("a", this).text(...)

Also a note, generally you shouldn't capitalize ids/classes in html 还要注意,通常您不应该在html中大写id / classs

Update: here's a demo fiddle 更新:这是一个演示小提琴

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

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