简体   繁体   English

如何在jQuery更改时获得ul列表的值?

[英]How to get the value of the ul list on change with jQuery?

I have this list: 我有这个清单:

<ul class="country-list hide">
    <li class="country" data-dial-code="213" data-country-code="dz"><div class="flag-box"><div class="iti-flag dz"></div></div><span class="country-name">Algeria (‫الجزائر‬‎)</span><span class="dial-code">+213</span></li>
    <li class="country" data-dial-code="376" data-country-code="ad"><div class="flag-box"><div class="iti-flag ad"></div></div><span class="country-name">Andorra</span><span class="dial-code">+376</span></li>
    <li class="country" data-dial-code="244" data-country-code="ao"><div class="flag-box"><div class="iti-flag ao"></div></div><span class="country-name">Angola</span><span class="dial-code">+244</span></li>
</ul>

And I want to check which one is selected and get the data-country-code from it, Any body have any idea on how to do it with jQuery? 我想检查选择了哪一个,并从中获取数据国家代码。有人对如何使用jQuery有任何想法吗?

Selected elements have two values to access. 所选元素有两个值可访问。

One is "Val" and one is 'Text" 一个是“ Val”,一个是“ Text”

to get Value: $('ul.country-list').val(); 获取值: $('ul.country-list').val();

to get Text: $('ul.country-list').text(); 获取文本: $('ul.country-list').text();

You can try with this 你可以尝试一下

$("ul.country-list option:selected").attr("data-country-code").val();

I think there is some confusion related to "change" and ":selected" word. 我认为“更改”和“:已选择”一词有些混淆。 These work with select Tag. 这些与选择标签一起使用。 But. 但。 for your query answer is this with jQuery: 为您的查询答案是jQuery的:

<script>
$(document).ready(function(){
  $('.country-list li').click(function(){
    var dataVal = $(this).data('dial-code');
    alert(dataVal);
   });
});
</script>

Hope, you have got the answer. 希望,你得到了答案。

As Others comment that their is no options to select so we need to do this by add class on current click list element and I have update some html code in your then its work. 由于其他人评论说他们是没有选择的选择,因此我们需要通过在当前点击列表元素上添加类来完成此操作,并且我已经在您的工作中更新了一些html代码。

Html: HTML:

<ul class="country-list hide">
<li class="country">
    <div class="flag-box">
      <div class="iti-flag dz"></div>
     </div>
     <span class="country-name" data-country-code="dz">Algeria (‫الجزائر‬‎)</span>
     <span class="dial-code" data-dial-code="213" >+213</span>
 </li>
<li class="country">
    <div class="flag-box">
      <div class="iti-flag ad"></div>
     </div>
     <span class="country-name" data-country-code="ad" >Andorra</span>
     <span class="dial-code"  data-dial-code="376" >+376</span>
</li>
<li class="country">
    <div class="flag-box">
      <div class="iti-flag ao"></div>
    </div>
    <span class="country-name" data-country-code="ao">Angola</span>
    <span class="dial-code" data-dial-code="244" >+244</span>
 </li>
</ul>

why i change data-attr position to span 为什么我将数据属性位置更改为跨度

Jquery: jQuery:

$('ul.country-list li').on('click',function(){

$('ul.country-list>li').removeClass('active');

var dailCode = $(this).addClass('active').find('[data-dial-code]').attr('data-dial-code');

var dataCountryCode = $(this).find('[data-country-code]').attr('data-country-code');

alert(dailCode+' Country Code '+dataCountryCode);
});



Some Css: 一些CSS:

ul.country-list>li {
  cursor:pointer;
}

ul.country-list li.active span {
 color: red;
}



And Here is JS Fiddle 这是JS Fiddle

When you click on any li that time you just need to add a class like "active" and remove active class from other li's if any. 那个时候单击任何一个li时,您只需要添加一个类似“ active”的类,并从其他li的活动类(如果有)中删除active类。 By this you can get the value of selected li. 通过这种方式,您可以获得所选li的值。

$('.active .country-name').html();

and

$('.active .dial-code').html();

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

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