Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Dynamiczne dodawanie formularza, problem z klasą
Jarod
post 5.02.2008, 20:14:10
Post #1





Grupa: Zarejestrowani
Postów: 1 190
Pomógł: 27
Dołączył: 23.04.2005

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


Domyślne mój formularz zawiera 2 pola input, które rozłożone są obok siebie.
Zastosowałem poniższy skrypt, aby dodawać kolejne wiersze z inputami.
Problem w tym, że ten skrypt, generuje coś takiego:
Kod
<input class="dimentions_length_input" type="text" name="iLength" value="" maxlength="4" />
<input class="dimentions_width_input" type="text" name="iWidth" value="" maxlength="4" />


a potrzebuję coś takiego:
Kod
<p class="dimentions_length_input"><input type="text" name="iLength" value="" maxlength="4" /></p>
<p class="dimentions_width_input"><input type="text" name="iWidth" value="" maxlength="4" /></p>


Funkcja JS:
  1. function addInputs(id)
  2. {
  3. var element = document.createElement('input');
  4. element.setAttribute('type', 'text');
  5. element.setAttribute('name', 'iLength');
  6. element.setAttribute('maxlength', '4');
  7. element.className = 'dimentions_length_input';
  8. var container = document.getElementById(id);
  9. container.appendChild(element);
  10.  
  11. var element = document.createElement('input');
  12. element.setAttribute('type', 'text');
  13. element.setAttribute('name', 'iWidth');
  14. element.setAttribute('maxlength', '4');
  15. element.className = 'dimentions_width_input';
  16. var container = document.getElementById(id);
  17. container.appendChild(element);
  18. }



Raczkuję dopiero w JS i nie wiem czy jest coś takiego możliwe? Próbowałem przed element.setAttribute('type', 'text'); wywoływać coś ala
element.= '<p class="dimentions_length_input">';
ale oczywiście nie działa.

Ktoś pomoże?

Pozdrawiam


--------------------
”Godzina nauki w życiu nowoczesnego apostoła jest godziną modlitwy.”
(św. Josemaría Escrivá, Droga, 335)
Go to the top of the page
+Quote Post
nowotny
post 5.02.2008, 20:44:07
Post #2





Grupa: Zarejestrowani
Postów: 875
Pomógł: 122
Dołączył: 2.02.2008

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


Zobacz czy coś takiego będzie działać:
  1. function addInputs(id)
  2. {
  3. var container = document.getElementById(id);
  4. for(var i=0;i<2;i++){
  5.  
  6. var par = document.createElement('p');
  7. par.className = 'dimentions_length_input';
  8. container.appendChild(par);
  9.  
  10. var element = document.createElement('input');
  11. element.setAttribute('type', 'text');
  12. element.setAttribute('name', 'iLength');
  13. element.setAttribute('maxlength', '4');
  14. par.appendChild(element);
  15. }
  16.  
  17. }


Ten post edytował nowotny 5.02.2008, 20:49:22
Go to the top of the page
+Quote Post
Jarod
post 5.02.2008, 20:47:18
Post #3





Grupa: Zarejestrowani
Postów: 1 190
Pomógł: 27
Dołączył: 23.04.2005

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


Wogóle nie działa..


--------------------
”Godzina nauki w życiu nowoczesnego apostoła jest godziną modlitwy.”
(św. Josemaría Escrivá, Droga, 335)
Go to the top of the page
+Quote Post
nowotny
post 5.02.2008, 20:50:04
Post #4





Grupa: Zarejestrowani
Postów: 875
Pomógł: 122
Dołączył: 2.02.2008

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


Cytat(Jarod @ 5.02.2008, 20:47:18 ) *
Wogóle nie działa..

3 edity były więc teraz sprawdź... smile.gif
Go to the top of the page
+Quote Post
Jarod
post 5.02.2008, 20:59:43
Post #5





Grupa: Zarejestrowani
Postów: 1 190
Pomógł: 27
Dołączył: 23.04.2005

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


Dziękuje.

Edit: W jaki sposób wyświetlić na ekranie zawartość "element"? Przez alert nie działa bo to obiekt?
Pytam się ponieważ mi coś nie pasuje.
Skoro najpierw tworzysz element <p> i dodajesz go, następnie dodajesz inputa, to dlaczego nie zamykasz elementu </p> ?
Chciałbym mieć pewność, że input rzeczywiście zostaje utworzony pomiędzy znacznikami <p></p>

pzdr

Ten post edytował Jarod 5.02.2008, 21:08:31


--------------------
”Godzina nauki w życiu nowoczesnego apostoła jest godziną modlitwy.”
(św. Josemaría Escrivá, Droga, 335)
Go to the top of the page
+Quote Post
nowotny
post 5.02.2008, 21:22:17
Post #6





Grupa: Zarejestrowani
Postów: 875
Pomógł: 122
Dołączył: 2.02.2008

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


Cytat(Jarod @ 5.02.2008, 20:59:43 ) *
Skoro najpierw tworzysz element <p> i dodajesz go, następnie dodajesz inputa, to dlaczego nie zamykasz elementu </p> ?

Bo Javascript robi to za mnie...

Cytat(Jarod @ 5.02.2008, 20:59:43 ) *
Chciałbym mieć pewność, że input rzeczywiście zostaje utworzony pomiędzy znacznikami <p></p>


Sprawdź sobie jakimś DOM inspectorem...
http://img216.imageshack.us/img216/329/2008020500uc9.png
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: 25.07.2025 - 10:09