如何在jQuery中获取元素值

为什么它不会在’li’中返回值? 我究竟做错了什么?

$("#list li").click(function() { var selected = $(this).val(); alert(selected); }) 

您想要li标签内的HTML或文本吗?

如果是这样,请使用:

 $(this).html() 

要么:

 $(this).text() 

val()仅用于表单字段。

 
  • Whatever
  • . . $('#unOrderedList li').click(function(){ var value = $(this).attr('value'); alert(value); });

您在“li”标记内查找属性“value”

使用.text()或.html()

 $("#list li").click(function() { var selected = $(this).text(); alert(selected); }); 

李没有价值。 只有与表单相关的元素(如input,textarea和select)才具有值。

最有可能的是,你想要这样的东西:

 $("#list li").click(function() { var selected = $(this).html(); alert(selected); }); 
  

Liste des Produits

$(document).ready(function(){ $(".inter li").bind( "click", function(){ alert($(this).children("a").text()); }); });

你可以在()上使用jQuery来做同样的事情。

 $("#list").on('click','li',(function() { var selected = $(this).text(); //or .html() alert(selected); }) 
 $("#list li").click(function() { var selected = $(this).html(); alert(selected); }); 

要获得li的值,我们应该使用$(“#list li”)。click(function(){var selected = $(this).html();

要么

 var selected = $(this).text(); alert(selected); 

})