Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: JS - document.getElementById
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
michas61
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
Cezar708
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
michas61
No chyba nie zrozumialeś o co mi biega. Chodzi o przekazywanie parametru z funkcji do document.getElementById(tutaj ma byc parametr)
nospor
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.
Cezar708
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ąć.
michas61
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.
Cezar708
cudów nie ma, pokaż kod!
michas61
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();
}
Cezar708
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.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.