Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [HTML][JavaScript]Problem z select i jquery
elnino17
post 12.04.2014, 19:21:27
Post #1





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 2.10.2012

Ostrzeżenie: (10%)
X----


Co jest nie tak z kodem że nic nie wyświetla ? #ulga, jest utworzone
  1. $(document).ready(function () {
  2. switch (parseInt($('#ulga').val())) {
  3. case "a" :
  4. var cenaZulga = cenaPodstawowa;
  5. document.getElementById('cenaZulga').innerHTML = cenaZulga;
  6. break;
  7. case "b" :
  8. var cenaZulga = cenaPodstawowa*0.37;
  9. document.getElementById('cenaZulga').innerHTML = cenaZulga;
  10. break;
  11. case "c" :
  12. var cenaZulga = cenaPodstawowa*0.51;
  13. document.getElementById('cenaZulga').innerHTML = cenaZulga;
  14. break;
  15. case "d" :
  16. var cenaZulga = cenaPodstawowa*0.37;
  17. document.getElementById('cenaZulga').innerHTML = cenaZulga;
  18. break;
  19. default:
  20.  
  21. }

  1. <form action="#" onsubmit="showLocation(); return false;">
  2. <select id="ulga" name="ulga">
  3. <option value="a">-</option>
  4. <option value="b">Uczeń - ulga 37%</option>
  5. <option value="c">Student - ulga 51%</option>
  6. <option value="d">Emeryt - ulga 37%</option>
  7. </select>
  8. <tr>
  9. <td>Miejscowość początkowa</td>
  10. <td>
  11. <input type="text" name="address1" class="field">
  12. </td>
  13. </tr>
  14. <tr>
  15. <td>Miejscowość końcowa</td>
  16. <td>
  17. <input type="text" name="address2" class="field">
  18. </td>
  19. </tr>
  20. </table>
  21. <button type="submit" class="submit"><b>Zobacz cenę biletu</b></button>
  22. </form>
  23.  
  24.  


Ten post edytował elnino17 12.04.2014, 19:23:06
Go to the top of the page
+Quote Post
trueblue
post 12.04.2014, 19:26:30
Post #2





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Sprawdź co zwraca funkcja parseInt ze stringa typowo tekstowego.

Ten post edytował trueblue 12.04.2014, 19:26:41


--------------------
Go to the top of the page
+Quote Post
elnino17
post 12.04.2014, 20:45:11
Post #3





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 2.10.2012

Ostrzeżenie: (10%)
X----


Zmieniłem ale wyświetla tylko wartość 10, mimo zmiany listy rozwijanej i wysłania formularza
  1. <!DOCTYPE html>
  2. <head>
  3. <title>Cennik biletów</title>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  7. var cenaPodstawowa = 10.00;
  8. $(document).ready(function () {
  9. switch (parseInt($('#ulga').val())) {
  10. case 1 :
  11. var cenaZulga = cenaPodstawowa;
  12. document.getElementById('cenaZulga').innerHTML = cenaZulga;
  13. console.log(cenaZulga);
  14. break;
  15. case 2 :
  16. var cenaZulga = cenaPodstawowa*0.37;
  17. document.getElementById('cenaZulga').innerHTML = cenaZulga;
  18. console.log(cenaZulga);
  19. break;
  20. case 3 :
  21. var cenaZulga = cenaPodstawowa*0.51;
  22. document.getElementById('cenaZulga').innerHTML = cenaZulga;
  23. console.log(cenaZulga);
  24. break;
  25. case 4 :
  26. var cenaZulga = cenaPodstawowa*0.37;
  27. document.getElementById('cenaZulga').innerHTML = cenaZulga;
  28. console.log(cenaZulga);
  29. break;
  30.  
  31. }
  32. });
  33.  
  34.  
  35. </script>
  36. </head>
  37. <body>
  38. <form>
  39. <select id="ulga">
  40. <option value="01">-</option>
  41. <option value="02">Uczeń - ulga 37%</option>
  42. <option value="03">Student - ulga 51%</option>
  43. <option value="04">Emeryt - ulga 37%</option>
  44. </select>
  45. <a href="#">Open</a>
  46. </form>
  47. <h1 id="cenaZulga"></h1>
  48.  
  49. </body>
  50. </html>
  51.  
Go to the top of the page
+Quote Post
ber32
post 12.04.2014, 20:51:45
Post #4





Grupa: Zarejestrowani
Postów: 332
Pomógł: 22
Dołączył: 6.07.2010

Ostrzeżenie: (0%)
-----


Zrób tak
do każdego tego elementu przypisz inną cenę np
  1. document.getElementById('cenaZulga').innerHTML = 5;

potem zmień wszystkie value="01" na value="1" itd...
i napisz czy ok

Ten post edytował ber32 12.04.2014, 20:53:49


--------------------
Go to the top of the page
+Quote Post
elnino17
post 12.04.2014, 21:01:14
Post #5





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 2.10.2012

Ostrzeżenie: (10%)
X----


Zrobiłem tak:
  1. case 2 :
  2. var cenaZulga = cenaPodstawowa;
  3. document.getElementById('cenaZulga').innerHTML = 27;
  4. console.log(cenaZulga);
  5. break;

  1. <option value = "2">Uczeń - ulga 37%</option>

i nie działa.
Go to the top of the page
+Quote Post
ber32
post 13.04.2014, 00:03:43
Post #6





Grupa: Zarejestrowani
Postów: 332
Pomógł: 22
Dołączył: 6.07.2010

Ostrzeżenie: (0%)
-----


No i co zero inicjatywy. ohno-smiley.gif

  1. <!DOCTYPE html>
  2. <title>Cennik biletów</title>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  6. $(document).ready(function () {
  7. var cenaPodstawowa = 10.00;
  8. $( "select" )
  9. .change(function() {
  10. $('#ulga option:selected').each(function() {
  11. var x = $( this ).val();
  12. switch (parseInt(x)) {
  13. case 1 :
  14. var cenaZulga = cenaPodstawowa;
  15. $( "#cenaZulga" ).text( cenaZulga );
  16. console.log(cenaZulga);
  17. break;
  18. case 2 :
  19. var cenaZulga = cenaPodstawowa*0.37;
  20. $( "#cenaZulga" ).text( cenaZulga );
  21. console.log(cenaZulga);
  22. break;
  23. case 3 :
  24. var cenaZulga = cenaPodstawowa*0.51;
  25. $( "#cenaZulga" ).text( cenaZulga );
  26. console.log(cenaZulga);
  27. break;
  28. case 4 :
  29. var cenaZulga = cenaPodstawowa*0.399;
  30. $( "#cenaZulga" ).text( cenaZulga );
  31. console.log(cenaZulga);
  32. break;
  33. }
  34. });
  35. });
  36. });
  37. </head>
  38. <select id="ulga">
  39. <option value="01">-</option>
  40. <option value="02">Uczeń - ulga 37%</option>
  41. <option value="03">Student - ulga 51%</option>
  42. <option value="04">Emeryt - ulga 37%</option>
  43. <a href="#">Open</a>
  44. </form>
  45. <h1 id="cenaZulga"></h1>
  46.  
  47. </body>
  48. </html>


I daj więcej ulgi na emerytów bo są biedni na leki nie mają, a dla studentów przynajmniej 90% oneeyedsmiley02.png

Ten post edytował ber32 13.04.2014, 00:06:28


--------------------
Go to the top of the page
+Quote Post
elnino17
post 13.04.2014, 20:22:22
Post #7





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 2.10.2012

Ostrzeżenie: (10%)
X----


Dzięki, wszystko działa, temat do zamknięcia smile.gif

Ten post edytował elnino17 14.04.2014, 06:17:20
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.04.2024 - 14:07