Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Java]wczytywanie do inputa w zależności od wyboru option select.
casperii
post 26.11.2015, 20:19:19
Post #1





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


Panowie mam 3 wartości które w zależności od selecta chciałbym wczytać do inputa.

  1. $('input[name="price1"]').val(Math.ceil(dolarPlusMarza));
  2. $('input[name="price1"]').val(Math.ceil(euroPlusMarza));
  3. $('input[name="price1"]').val(Math.ceil(funtPlusMarza));
  4.  
  5. var select = document.getElementById('waluta'),
  6. onChange = function(event) {
  7. }


No właśnie ale dalej nie wiem jak to ugryźć. Pomoże ktoś?
Czyli z selecta waluta wybiore DOL wczyta mi się do inputa price1 wartość dolarPlusMarza itd.
Go to the top of the page
+Quote Post
dgladys
post 26.11.2015, 22:45:50
Post #2





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 26.11.2015

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


Przygotowałem przykład na jsfiddle

https://jsfiddle.net/qmnap87s/

  1. var eurCurrency = 4.56;
  2. var usdCurrency = 3.11;
  3.  
  4. document.getElementById('currencyChoice').onchange = function() {
  5. var currencyType = this.value;
  6. var currencyValue = 0.00;
  7. switch(currencyType) {
  8. case "EUR": currencyValue = eurCurrency; break;
  9. case "USD": currencyValue = usdCurrency; break;
  10. default: return;
  11. }
  12.  
  13. document.getElementById('currencyValueInput').value = currencyValue;
  14. }
Go to the top of the page
+Quote Post
casperii
post 26.11.2015, 23:18:50
Post #3





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


Dzięki za pomoc @dgladys, chociaż zastanawiam się w jaki sposób mogę przenieść wartość z jednej funkcji do drugiej:

  1. function fill(thisValue) {
  2. $('#product').val(thisValue);
  3. setTimeout("$('#suggestions').fadeOut();", 600);
  4. $.getJSON('ajax2.php', {input: ""+thisValue+""}, function(data) {
  5. var dolar = Math.ceil((data.namePrice)/(<?=$kurs_dolara;?>));
  6. var euro = Math.ceil((data.namePrice)/(<?=$kurs_euro;?>));
  7. var funt = Math.ceil((data.namePrice)/(<?=$kurs_funta;?>));
  8. });
  9. }
  10.  
  11. window.onload=function(){
  12. var usdCurrency = 3.11; // tu chce zrobić var usdCurrency = dolar
  13. var eurCurrency = 4.56;
  14. var gbpCurrency = 5.56;
  15.  
  16. document.getElementById('currencyChoice').onchange = function() {
  17. var currencyType = this.value;
  18. var currencyValue = 0.00;
  19. switch(currencyType) {
  20. case "us": currencyValue = usdCurrency; break;
  21. case "euro": currencyValue = eurCurrency; break;
  22. case "gbp": currencyValue = gbpCurrency; break;
  23. default: return;
  24. }
  25.  
  26. document.getElementById('currencyValueInput').value = currencyValue;
  27. }
  28. }


chce zrobić var usdCurrency = dolar
w jaki sposób pobrać wartość z pierwszej funkcji do drugiej.
Go to the top of the page
+Quote Post
by_ikar
post 27.11.2015, 08:09:36
Post #4





Grupa: Zarejestrowani
Postów: 1 798
Pomógł: 307
Dołączył: 13.05.2009
Skąd: Gubin/Wrocław

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




ludzie chyba się nigdy nie nauczą..
Go to the top of the page
+Quote Post
casperii
post 29.11.2015, 17:45:46
Post #5





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


@by_ikar wiem, że ajax i js to słabe rozwiązanie, ale zapewne jakoś rozwiązać to można.

w inpucie wyszukuje interesujący mnie produkt który mi m.in pobiera cene w pln którą przeliczam na USD, EUR, GBP

  1. var dolar = Math.ceil((data.namePrice)/(<?=$kurs_dolara;?>));
  2. var euro = Math.ceil((data.namePrice)/(<?=$kurs_euro;?>));
  3. var funt = Math.ceil((data.namePrice)/(<?=$kurs_funta;?>));


na upartego te wartości mogę wczytać do input type hidden prawda?
A później przy pomocy JS wczytać poszczególny input w zależności od wyboru waluty z selecta.


Dobra zrobiłem na chwilę obecną coś takiego:

  1. $('input[name="test"]').val(Math.ceil(data.namePrice));


powyżej wczytuje mi do input type hidden.

Następnie tą wartość sobie kopiuje do zmiennej pln.

  1. $("html").bind({
  2. click:function() {
  3. var pln = document.getElementById('test').value;
  4. }
  5. });


Ale teraz za cholere nie wiem jak to zmienną przekazać tutaj:

  1. window.onload=function(){
  2. var pln = pln // tu chce wczytać tą wartość.
  3. var usdCurrency = 4.22;
  4. var eurCurrency = 4.56;
  5. var gbpCurrency = 5.56;
  6. var plnCurrency = pln;
  7.  
  8. document.getElementById('currencyChoice').onchange = function() {
  9. var currencyType = this.value;
  10. var currencyValue = 0.00;
  11.  
  12. switch(currencyType) {
  13. case "us": currencyValue = usdCurrency; break;
  14. case "euro": currencyValue = eurCurrency; break;
  15. case "gbp": currencyValue = gbpCurrency; break;
  16. case "pln": currencyValue = plnCurrency; break;
  17. default: return;
  18. }
  19.  
  20. document.getElementById('currencyValueInput').value = currencyValue;
  21. }
  22. }


ma ktoś jakiś pomysł ?

Ten post edytował casperii 29.11.2015, 16:08:11
Go to the top of the page
+Quote Post
com
post 29.11.2015, 20:55:22
Post #6





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

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


czemu po prostu nie zrobisz document.getElementById('test').value; ?

tylko czemu mieszasz js z jq? używaj albo tego albo tego, bo tak to trochę bez sensu biggrin.gif nwm czy dobrze zrozumiałem problem smile.gif
Go to the top of the page
+Quote Post
casperii
post 29.11.2015, 21:08:19
Post #7





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


@com dane odebrałem ajaxem , później (tak mi się wydaje) obrabiam 1 wartość na poszczególne waluty i tu bezsensu przesyłać ponownie ajaxem.

Czyli po wpisaniu w input nazwy produktu otrzymuje jakieś tam informacje i m.in cene w hidden input.
Niestety tej wartości nie mogę odebrać poza funkcje ajaxa. Stąd wymyśliłem sobie, że skopiuje ukrytego inputa

  1. $("html").bind({
  2. click:function() {
  3. var pln = document.getElementById('test').value;
  4. }
  5. });


teraz ta cena jest w wartości pln.
I moje pytanie brzmi jak tą wartość pln przenieść/odczytać w kolejnej funkcji:

  1. window.onload=function(){
  2. var pln = pln // tu chce wczytać tą wartość.
  3. var usdCurrency = 4.22;
  4. var eurCurrency = 4.56;
  5. var gbpCurrency = 5.56;
  6. var plnCurrency = pln;
  7.  
  8. document.getElementById('currencyChoice').onchange = function() {
  9. var currencyType = this.value;
  10. var currencyValue = 0.00;
  11.  
  12. switch(currencyType) {
  13. case "us": currencyValue = usdCurrency; break;
  14. case "euro": currencyValue = eurCurrency; break;
  15. case "gbp": currencyValue = gbpCurrency; break;
  16. case "pln": currencyValue = plnCurrency; break;
  17. default: return;
  18. }
  19.  
  20. document.getElementById('currencyValueInput').value = currencyValue;
  21. }
  22. }

w drugiej odsłonie var pln ma pustke sad.gif
  1. var pln = document.getElementById('test').value;


lub

  1. var pln = pln;


Ten post edytował casperii 29.11.2015, 21:12:38
Go to the top of the page
+Quote Post
com
post 29.11.2015, 21:20:01
Post #8





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

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


ale w onload nie będzie wartości bo ajax się nie wykonał jeszcze. Nie zapominaj ze ajax to asynchroniczne zapytania. Zrób sobie zmienna globalna wtedy masz do niej dostęp poza funkcja jak na jsfiddle smile.gif
Go to the top of the page
+Quote Post
casperii
post 29.11.2015, 21:37:04
Post #9





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


@com coś nie idzie z tymi globalnymi:

  1. var zmienna;
  2. function fill(thisValue) {
  3. $('#product').val(thisValue);
  4. setTimeout("$('#suggestions').fadeOut();", 600);
  5. $.getJSON('ajax2.php', {input: ""+thisValue+""}, function(data) {
  6. zmienna = data.namePrice;
  7. alert('zmienna jest'+zmienna);
  8. });
  9. }
  10.  
  11. $("html").bind({
  12. click:function() {
  13. alert('pokaz mi'+zmienna);
  14.  
  15. }
  16. });


Ok w drugiej funkcji przy click alert mi pokazuje zmienną. Teraz dalej chciałbym odebrać tą zmienną tutaj:

  1. window.onload=function(){
  2. var plnCurrency = zmienna;
  3.  
  4. document.getElementById('currencyChoice').onchange = function() {
  5. var currencyType = this.value;
  6. var currencyValue = 0.00;
  7.  
  8. switch(currencyType) {
  9. case "us": currencyValue = usdCurrency; break;
  10. case "euro": currencyValue = eurCurrency; break;
  11. case "gbp": currencyValue = gbpCurrency; break;
  12. case "pln": currencyValue = plnCurrency; break;
  13. default: return;
  14. }
  15.  
  16. document.getElementById('currencyValueInput').value = currencyValue;
  17. }
  18. }


Ale tu już raczej nie jest możliwe pobranie tej wartości

Ten post edytował casperii 29.11.2015, 22:00:45
Go to the top of the page
+Quote Post
com
post 29.11.2015, 21:39:51
Post #10





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

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


bo nie mozesz robić var zmienna w fill, tylko robisz zmienna = data.namePrice;
bo tak robisz lokalna zmienna o nazwie zmienna smile.gif

Ten post edytował com 29.11.2015, 21:40:07
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: 23.05.2025 - 06:49