Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> JS - document.getElementById
michas61
post 11.03.2008, 23:02:20
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 27.09.2005

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


Witam

Mam pewien problem :
wywołuje sobie ajax.onCompletion = createInwent('uzytkownik');

Następnie mam funkcję:

function createInwent(akcja) {
var obj = document.getElementById(akcja);
}

chciałbym się dowiedzieć w jaki sposób przekazać parametr "uzytkownik" tak,
aby document.getElementById(akcja) działał poprawnie.

Próbowałem już różnych kombinacji:
document.getElementById('+akcja+')
ale bez rezultatu.

Bardzo proszę o jakieś pomysły.
Pozdrawiam
Go to the top of the page
+Quote Post
Cezar708
post 11.03.2008, 23:09:03
Post #2





Grupa: Zarejestrowani
Postów: 1 116
Pomógł: 119
Dołączył: 10.05.2005
Skąd: Poznań

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


coś chyba mylisz:

document.getElementById(id) pobiera element DOM o danym id z Twojej strony. Nie wiem czy czasem czegoś nie mylisz nazywając to `akcją`.

Proponuję abyś poczytał sobie tu: http://www.w3schools.com/htmldom/met_doc_getelementbyid.asp

pozdrawiam
Go to the top of the page
+Quote Post
michas61
post 11.03.2008, 23:14:52
Post #3





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 27.09.2005

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


No chyba nie zrozumialeś o co mi biega. Chodzi o przekazywanie parametru z funkcji do document.getElementById(tutaj ma byc parametr)
Go to the top of the page
+Quote Post
nospor
post 11.03.2008, 23:22:03
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Kod
function createInwent(idpola) {
var obj = document.getElementById(idpola);
}

To jest poprawne przekazenie zmiennej i wstawienie dla getElementById.

A czy te createInwent ma cos zwracac? To zapomniales o return.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Cezar708
post 11.03.2008, 23:27:09
Post #5





Grupa: Zarejestrowani
Postów: 1 116
Pomógł: 119
Dołączył: 10.05.2005
Skąd: Poznań

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


Cytat
ajax.onCompletion = createInwent('uzytkownik');
function createInwent(akcja) {
var obj = document.getElementById(akcja);
}

no toć to jest poprawne przekazanie parametru do funkcji może napisz co chcesz osiągnąć.
Go to the top of the page
+Quote Post
michas61
post 11.03.2008, 23:30:40
Post #6





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 27.09.2005

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


No też tak właśnie myślałem, że taka deklaracja jest poprawna, ale okazuje się ze w tym przypadku nie działa.
Nie mam pomysłu. Gdy daje wprost jawnie document.getElementById('jakis text') to działa dobrze, jak wkładam parametr nie.
Go to the top of the page
+Quote Post
Cezar708
post 11.03.2008, 23:32:11
Post #7





Grupa: Zarejestrowani
Postów: 1 116
Pomógł: 119
Dołączył: 10.05.2005
Skąd: Poznań

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


cudów nie ma, pokaż kod!
Go to the top of the page
+Quote Post
michas61
post 11.03.2008, 23:36:56
Post #8





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 27.09.2005

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


No to się lekko post wydłuży smile.gif

plik: funckcje.js -> include w HEAD

CODE


var ajax = new sack();

function user(sel) {
var user_ID = sel.options[sel.selectedIndex].value;
document.getElementById('umowa').options.length = 0;
if (user_ID.length > 0){
ajax.requestFile = '/modules/function/testuj.php?akcja=uzytkownik';
ajax.onCompletion = create_user('umowa');
ajax.runAJAX();
}
}
function create_user(akcja) {
var obj = document.getElementById(''+akcja);
window.alert(obj);
eval(ajax.response);
}



plik select_list.js -> include w HEAD

CODE

function sack(file) {
this.xmlhttp = null;

this.resetData = function() {
this.method = "POST";
this.queryStringSeparator = "?";
this.argumentSeparator = "&";
this.URLString = "";
this.encodeURIString = true;
this.execute = false;
this.element = null;
this.elementObj = null;
this.requestFile = file;
this.vars = new Object();
this.responseStatus = new Array(2);
};

this.resetFunctions = function() {
this.onLoading = function() { };
this.onLoaded = function() { };
this.onInteractive = function() { };
this.onCompletion = function() { };
this.onError = function() { };
this.onFail = function() { };
};

this.reset = function() {
this.resetFunctions();
this.resetData();
};

this.createAJAX = function() {
try {
this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
this.xmlhttp = null;
}
}

if (! this.xmlhttp) {
if (typeof XMLHttpRequest != "undefined") {
this.xmlhttp = new XMLHttpRequest();
} else {
this.failed = true;
}
}
};

this.setVar = function(name, value){
this.vars[name] = Array(value, false);
};

this.encVar = function(name, value, returnvars) {
if (true == returnvars) {
return Array(encodeURIComponent(name), encodeURIComponent(value));
} else {
this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
}
}

this.processURLString = function(string, encode) {
encoded = encodeURIComponent(this.argumentSeparator);
regexp = new RegExp(this.argumentSeparator + "|" + encoded);
varArray = string.split(regexp);
for (i = 0; i < varArray.length; i++){
urlVars = varArray[i].split("=");
if (true == encode){
this.encVar(urlVars[0], urlVars[1]);
} else {
this.setVar(urlVars[0], urlVars[1]);
}
}
}

this.createURLString = function(urlstring) {
if (this.encodeURIString && this.URLString.length) {
this.processURLString(this.URLString, true);
}

if (urlstring) {
if (this.URLString.length) {
this.URLString += this.argumentSeparator + urlstring;
} else {
this.URLString = urlstring;
}
}

// prevents caching of URLString
this.setVar("rndval", new Date().getTime());

urlstringtemp = new Array();
for (key in this.vars) {
if (false == this.vars[key][1] && true == this.encodeURIString) {
encoded = this.encVar(key, this.vars[key][0], true);
delete this.vars[key];
this.vars[encoded[0]] = Array(encoded[1], true);
key = encoded[0];
}

urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
}
if (urlstring){
this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
} else {
this.URLString += urlstringtemp.join(this.argumentSeparator);
}
}

this.runResponse = function() {
eval(this.response);
}

this.runAJAX = function(urlstring) {
if (this.failed) {
this.onFail();
} else {
this.createURLString(urlstring);
if (this.element) {
this.elementObj = document.getElementById(this.element);
}
if (this.xmlhttp) {
var self = this;
if (this.method == "GET") {
totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
this.xmlhttp.open(this.method, totalurlstring, true);
} else {
this.xmlhttp.open(this.method, this.requestFile, true);
try {
this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
} catch (e) { }
}

this.xmlhttp.onreadystatechange = function() {
switch (self.xmlhttp.readyState) {
case 1:
self.onLoading();
break;
case 2:
self.onLoaded();
break;
case 3:
self.onInteractive();
break;
case 4:
self.response = self.xmlhttp.responseText;
self.responseXML = self.xmlhttp.responseXML;
self.responseStatus[0] = self.xmlhttp.status;
self.responseStatus[1] = self.xmlhttp.statusText;

if (self.execute) {
self.runResponse();
}

if (self.elementObj) {
elemNodeName = self.elementObj.nodeName;
elemNodeName.toLowerCase();
if (elemNodeName == "input"
|| elemNodeName == "select"
|| elemNodeName == "option"
|| elemNodeName == "textarea") {
self.elementObj.value = self.response;
}
else {
self.elementObj.innerHTML = self.response;
}
}
if (self.responseStatus[0] == "200") {
self.onCompletion();
} else {
self.onError();
}

self.URLString = "";
break;
}
};

this.xmlhttp.send(this.URLString);
}
}
};

this.reset();
this.createAJAX();
}
Go to the top of the page
+Quote Post
Cezar708
post 12.03.2008, 00:06:37
Post #9





Grupa: Zarejestrowani
Postów: 1 116
Pomógł: 119
Dołączył: 10.05.2005
Skąd: Poznań

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


ja tu widzę małą niezgodność w Twoim kodzie:

1. masz linię:
Kod
document.getElementById('umowa').options.length = 0;

co sugeruje że 'umowa' jest typu <select>

2. Następnie `skracasz` wszelkie optiony ze środka selecta

3. Kolejnym krokiem jest pobranie obiektu już w funkcji create_user()
Kod
var obj = document.getElementById(''+akcja);


4. i Wyewaluowanie pobranego przez ajax treści:
Kod
eval(ajax.response);


... może po prostu potrzebne Ci jest coś na styl innerHTML aby wrzucić treść do środka selekta?

a co Ci wyświetla alert? jest coś czy NULL? bo jeśli null to się zastanów czy obiekt 'umowa' w drzewie DOM w ogóle istnieje.
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: 18.07.2025 - 04:17