Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/05/2010, 14:46
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 2 meses
Puntos: 20
Como saber y enviar el valor del elemento seleccionado

Buenas,

No consigo saber el valor del elemento que tengo seleccionado. ¿Cuál es la forma correcta de hacerlo? he puesto un atributo title al elemento <li> pero a lo mejor se puede de otra forma?

De esta forma seleccione la opción que seleccione me dice que es "Diesel"

Código HTML:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/v1.98/DTD/v1.98-strict.dtd">
  2. <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
  3. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  4. <title>Marcando elementos</title>
  5. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
  6. <script type="text/javascript">
  7. $(document).ready(function() {
  8.     $("li").click( function () {
  9.         $(this).toggleClass('activo');
  10.         var n = $('.activo').length;
  11.         //$("span").text("Hay " + n + " elementos seleccionados");
  12.        
  13.         $("span").text("Valor del elemento seleccionado " + $("li").attr('title'));
  14.        
  15.     });
  16. });
  17. <style type="text/css">
  18.  
  19. body{
  20.     font-family:verdana;
  21.     size:12px;
  22.     margin:0 auto;
  23.     text-align:left;
  24. }
  25. .debug{
  26.     margin:50px 0 0 100px;
  27.     display:block;
  28. }
  29. .lista {
  30.     padding:10px;
  31.     margin:0px;
  32.     list-style:none;
  33. }
  34. .lista li {
  35.     display:block;
  36.     height:18px;
  37.     text-decoration:none;
  38.     color:#666666 !important;
  39.     padding:1px 5px 1px 5px;
  40.     border:1px solid #ffffff;
  41.    
  42. }
  43. .lista li:hover {
  44.     border:1px solid #67EF58;
  45.     text-decoration:none;
  46.     cursor:pointer;
  47. }
  48.  
  49. .activo{
  50.     background:#A4EF9C;
  51.     font-weight:bold;
  52.     color:#fff;
  53. }
  54. #simulador{
  55.     margin:60px 0 10px 100px;
  56.     width:500px;
  57. }
  58. .pregunta{
  59.     height:25px;
  60.     background:#eee;
  61.     text-align:left;
  62. }
  63. p.pregunta{
  64.     font-weight:bold;
  65.     padding:6px 0px 0px 15px;
  66.     margin:0;
  67. }
  68. #lista_preguntas{
  69.     border:1px solid #ccc;
  70.     padding:2px;
  71. }
  72. #progreso_simulador{
  73.     margin:60px 0 0 100px;
  74. }
  75. </head>
  76. <div id="progreso_simulador">Aqui va una barra de progreso...</div>
  77. <div id="simulador">
  78.     <p class="pregunta">¿Qué tipo de combustible usa tu coche?</p>
  79.     <div id="lista_preguntas">
  80.         <ul class="lista">
  81.             <li title="Diesel">Diesel</li>
  82.             <li title="Gasolina">Gasolina</li>
  83.         </ul>
  84.     </div>
  85. </div>
  86. <span class="debug"></span>
  87. </body>
  88. </html>

Muchas gracias de antemano!