2009-02-15 14 views

Respuesta

53

¿Desea el código HTML o el texto que está dentro de la etiqueta li?

Si es así, utilice uno:

$(this).html() 

o:

$(this).text() 

El val() es para campos de formulario solamente.

+0

¿Qué quiere decir con "campos de formulario"? ¿Puedes darme un ejemplo? tnx – stack

2

Un li no tiene ningún valor. Solo los elementos relacionados con el formulario como input, textarea y select tienen valores.

2

Lo más probable es que quieren algo como esto:

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

Uso .text() o .html()

$("#list li").click(function() { 
     var selected = $(this).text(); 
     alert(selected); 
}); 
6
<ul id="unOrderedList"> 
<li value="2">Whatever</li> 
. 
. 



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

Su buscando el atributo "value" en el interior del etiqueta "li"

0
$("#list li").click(function() { 
     var selected = $(this).html(); 
     alert(selected); 
}); 
1
<div class="inter"> 
     <p>Liste des Produits</p> 
     <ul> 
      <li><a href="#1">P1</a></li> 
      <li><a href="#2">P2</a></li> 
      <li><a href="#3">P3</a></li> 
     </ul> 
    </div> 

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

Puede hacer lo mismo utilizando jQuery on().

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

para obtener el valor de li debemos utilizar $ ("# lista li") haga clic en (function() {= $ (this) .html seleccionado var();.

o

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

alert(selected); 

})

Cuestiones relacionadas