Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript] warunek
tenloginjestzaje...
post
Post #1





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

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


Witam
Nie wiem jak wyświetlić swój tekst zamiast "NaN" jeśli w input jest 0 lub pusty tekst. Dzięki.
  1. <script>
  2. function oblicz(){
  3. var sum = document.getElementById('sum').value;
  4. return sum.toFixed(2);
  5. }
  6. </script>
  7. <span id="sum"></span>
  8. <input min="100" max="10000000" class="searchinput" id="sum" name="sum" />
  9. <input type="button" class="button" value="Przelicz"
  10. onclick="document.getElementById('oblicz')" />


Ten post edytował tenloginjestzajety 7.02.2017, 20:36:01
Go to the top of the page
+Quote Post
viking
post
Post #2





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Ale wiesz ze trzeba by funkcję wywołać a nie wybrać jakiegoś inputa?
Go to the top of the page
+Quote Post
tenloginjestzaje...
post
Post #3





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

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


oblicza wszystko prawidłowo tylko mam problem z NaN jeśli nie ma w polu input liczby. Nie wiem jak zrobić komunikat w takiej sytuacji, zablokować button.
Go to the top of the page
+Quote Post
Spawnm
post
Post #4





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




https://developer.mozilla.org/pl/docs/Web/J...e/Obiekty/isNaN
Go to the top of the page
+Quote Post
tenloginjestzaje...
post
Post #5





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

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


a jak to podstawić tutaj:
  1. <script>
  2. function oblicz(){
  3. var sum = document.getElementById('sum').value;
  4. return sum.toFixed(2);
  5. }
  6. if (isNaN(sum)) {
  7. sum = "0";
  8. } else {
  9. sum = "1";
  10. }
  11. }
  12. </script>
  13. <span id="sum"></span>
  14. <input min="100" max="10000000" class="searchinput" id="sum" name="sum" />
  15. <input type="button" class="button" value="Przelicz"
  16. onclick="document.getElementById('oblicz')" />
  17.  


Ten post edytował tenloginjestzajety 7.02.2017, 20:46:08
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 24.08.2025 - 18:32