¿Por qué no devuelve el valor en 'li'? ¿Qué estoy haciendo mal?Cómo obtener el valor del elemento en jQuery
$("#list li").click(function() {
var selected = $(this).val();
alert(selected);
})
¿Por qué no devuelve el valor en 'li'? ¿Qué estoy haciendo mal?Cómo obtener el valor del elemento en jQuery
$("#list li").click(function() {
var selected = $(this).val();
alert(selected);
})
¿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.
¿Qué quiere decir con "campos de formulario"? ¿Puedes darme un ejemplo? tnx – stack
Un li no tiene ningún valor. Solo los elementos relacionados con el formulario como input, textarea y select tienen valores.
Lo más probable es que quieren algo como esto:
$("#list li").click(function() {
var selected = $(this).html();
alert(selected);
});
Uso .text() o .html()
$("#list li").click(function() {
var selected = $(this).text();
alert(selected);
});
<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"
$("#list li").click(function() {
var selected = $(this).html();
alert(selected);
});
<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());
});
});
Puede hacer lo mismo utilizando jQuery on().
$("#list").on('click','li',(function() {
var selected = $(this).text(); //or .html()
alert(selected);
})
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);
})
¿Qué estás tratando de hacer? ¿Cómo se ve tu lista html? – markt