简体   繁体   English

如何在 jquery 中获取 UL / LI 值

[英]How to get UL / LI value in jquery

I need an array of Citys, how does it working in jquery?我需要一个城市数组,它如何在 jquery 中工作? I never touch frontend so I'm very newbie.我从不接触前端,所以我是个新手。

<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content" id="ui-id-180" tabindex="0" style="display: block; top: 166px; left: 1037.86px; width: 203px;">
<li class="ui-menu-item" id="ui-id-418" tabindex="-1">City1</li>
<li class="ui-menu-item" id="ui-id-419" tabindex="-1">City2</li>
<li class="ui-menu-item" id="ui-id-420" tabindex="-1">City3</li>
<li class="ui-menu-item" id="ui-id-421" tabindex="-1">City4</li></ul>

Also this list of citys is connected with input field.此城市列表也与输入字段相关联。 Values generated on server side.在服务器端生成的值。 Main UL has id "ui-id-180" is it possible it may change on user side?主要 UL 的 ID 为“ui-id-180”,它是否可能在用户端发生变化? Does input field connected with dropdown list?输入字段是否与下拉列表连接? If so is it possible to know which "ul li" connected with which field?如果是这样,是否有可能知道哪个“ul li”与哪个字段相关? My code to get value in input field:我在输入字段中获取值的代码:

$("[name=" + City.GetInputName() + "]").keyup(function(eventObject){
$("[name=" + Source.GetInputName() + "]").val(function(index, value){});});

Update 1 There is an example using @mplungjan answer.更新 1有一个使用@mplungjan 答案的示例 If there is more than one UL the array has unwanted values.如果有多个 UL,则数组具有不需要的值。 That's why I asked about connection of input field and UL ( to filtrate values).这就是为什么我询问输入字段和 UL(过滤值)的连接。

Here is how to get the cities in jQuery map without touching the ID以下是如何在不接触 ID 的情况下获取 jQuery 地图中的城市

 const cities = $(".ui-autocomplete li") .map(function() { return this.textContent.trim()}) .get(); console.log(cities)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content" id="ui-id-180" tabindex="0" style="display: block; top: 166px; left: 1037.86px; width: 203px;"> <li class="ui-menu-item" id="ui-id-418" tabindex="-1">City1</li> <li class="ui-menu-item" id="ui-id-419" tabindex="-1">City2</li> <li class="ui-menu-item" id="ui-id-420" tabindex="-1">City3</li> <li class="ui-menu-item" id="ui-id-421" tabindex="-1">City4</li> </ul>

if i understand the question correctly, you want to create an array containing the values of the li tags like this如果我正确理解了这个问题,你想创建一个包含li标签值的数组,像这样

["City1", "City2", "City3", "City4"]

a way to do this by using the map method like this一种通过使用这样的map方法来做到这一点的方法

//use HTML DOM to target the <li> elements
let li = document.getElementsByClassName("ui-menu-item");
//display the values into the array
let array = [...li].map(e => e.innerHTML);

console.log(array); 
//output : ["City1", "City2", "City3", "City4"]

Code should be:代码应该是:

var cities = [];
  $('#cities').click(function(){ \\ Click event
    $.each($('li'), function(index,el){ \\Loop on cities
        cities.push($(el).text()); \\Pushing city name to an array
    });
    alert(cities);
    console.log(cities);
  })

 var cities = []; $('#cities').click(function(){ $.each($('li'), function(index,el){ cities.push($(el).text()); }); alert(cities); console.log(cities); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content" id="ui-id-180" tabindex="0" style="display: block; top: 166px; left: 1037.86px; width: 203px;"> <li class="ui-menu-item" id="ui-id-418" tabindex="-1">City1</li> <li class="ui-menu-item" id="ui-id-419" tabindex="-1">City2</li> <li class="ui-menu-item" id="ui-id-420" tabindex="-1">City3</li> <li class="ui-menu-item" id="ui-id-421" tabindex="-1">City4</li></ul> <button id="cities">GET CITIES</button>

Hope this will be useful.希望这将是有用的。

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

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