Post
#1
|
|
|
Grupa: Zarejestrowani Postów: 5 Pomógł: 0 Dołączył: 28.11.2013 Ostrzeżenie: (0%)
|
Mam taki skrypt (przykład):
Kod var t = Object; for(w=0;w<10;w++){ t[w] = function(w){ alert(w) } } Chciałbym aby t[w] sprawiało że wyskakuje alert z zmienną w z pętli (tj t[1] = alert(1), t[2] = alert(2) itd), tylko function bierze w za parametr do nowej funkcji a nie jako zmienną z for. Próbowałem zrobić coś takiego: Kod var t = Object; for(w=0;w<10;w++){ t[w] = "alert(" + w + ");"; } Ale to nie działa (IMG:style_emoticons/default/sad.gif) Jak to zrobić? Jestem początkujący w JS Jakby był już taki temat to prosiłbym o podanie w poście linku i zamknięcie tego Szukałem na forum i znalazłem coś o callbackach ale nie wiem czy to rozwiązanie mojego problemu Ten post edytował glupik909 29.11.2013, 13:53:11 |
|
|
|
![]() |
Post
#2
|
|
|
Grupa: Zarejestrowani Postów: 5 Pomógł: 0 Dołączył: 28.11.2013 Ostrzeżenie: (0%)
|
Mam dwie wersje takiej funkcji ale obie zwracają undefined albo TypeError: document.forms[this.id] is undefined czy coś takiego (IMG:style_emoticons/default/thumbsdownsmileyanim.gif)
Kod function MyForm(id){ this.id = id; } MyForm.prototype.getChild = function(){ var string; for(t=0;t<document.forms[this.id].length;t++){ if(document.forms[this.id].children[t].constructor == HTMLInputElement && document.forms[this.id].children[t].type == "text"){ string += "form_field[" + t + "]=" + document.forms[this.id].children[t].value + "&"; } } return string.replace("undefined",""); } function callIt(id){ t = new MyForm(id); return t; } for(w=0;w<document.forms.length;w++){ document.forms[w].onsubmit = callIt(w).getChild; } function MyForm(id){ this.id = id; } MyForm.prototype.getChild = function(){ var string; for(t=0;t<document.forms[this.id].length;t++){ if(document.forms[this.id].children[t].constructor == HTMLInputElement && document.forms[this.id].children[t].type == "text" ){ string += "form_field[" + t + "]=" + document.forms[this.id].children[t].value + "&"; } } return string.replace("undefined",""); } function callIt(id){ t = new MyForm(id); return t; } var x = {} for(w=0;w<document.forms.length;w++){ x[w] = callIt(w) document.forms[w].onsubmit = x[w].getChild; } EDIT: Tema do zamknięcia, znalazłem rozwiązanie (IMG:style_emoticons/default/biggrin.gif) Kod function getChild(id){
var string; for(t=0;t<document.forms[id].children.length;t++){ if(document.forms[id].children[t].constructor == HTMLInputElement && document.forms[id].children[t].type == "text") string += "form_field[" + t + "]=" + document.forms[id].children[t].value + "&"; } } console.log(string.replace("undefined","")); } function setHandler(n,callback){ return function(){ callback(n); } } for(w=0;w<document.forms.length;w++){ document.forms[w].onsubmit = setHandler(w,getChild); } Ten post edytował glupik909 30.11.2013, 13:13:11 |
|
|
|
glupik909 Przekazywanie do nowej funkcji zmiennej z pętli for 29.11.2013, 13:43:38
werdan http://jsfiddle.net/N7xR2/ 29.11.2013, 14:02:26
freemp3 Nie potrzebnie przekazujesz "w" w parame... 29.11.2013, 14:07:39
glupik909 Thx
A możliwe byloby użycie tych rozwiązań do eve... 29.11.2013, 14:12:50
freemp3 Wszystkim formularzom jedną funkcje czy każdy ma m... 29.11.2013, 14:28:52 
glupik909 Cytat(freemp3 @ 29.11.2013, 15:28:52 ... 29.11.2013, 14:31:13
freemp3 Możesz to zrobić dodając do każdego formularza wyd... 29.11.2013, 15:27:59 
glupik909 Cytat(freemp3 @ 29.11.2013, 16:27:59 ... 29.11.2013, 15:43:57
freemp3 Ehh... Nie wyciągnąłeś wniosków z pierwszych dwóch... 29.11.2013, 16:00:03 ![]() ![]() |
|
Aktualny czas: 29.12.2025 - 18:50 |