Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> niezamierzone "czyszczenie" pól formularza
przemoos
post 15.07.2004, 15:27:39
Post #1





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 6.04.2002
Skąd: Lubaczow

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


Witam!

Napisalem prosty skrypt:

Kod
<script language="javascript">
    var countInputs = 0;
    function dodajInput()
    {
  document.getElementById('inputLayer').innerHTML+='<br /><input name="userfile_'+countInputs+'" type="file" />';
  countInputs++;
    }
</script>


i formularz do tego:
Kod
<form enctype="multipart/form-data" method="post">
<div id="inputLayer">
    <input type="hidden" name="MAX_FILE_SIZE" value="900000" />
</div>
    <input type="submit" value="Send File" />
</form>
<a href="javascript:dodajInput();">Dodaj pole formularza</a>


Problem w tym, że klikając w link dodający nowe pole formularza kasuje mi się wcześniej wpisana wartość w innych polach input. Jak to naprawić?
Go to the top of the page
+Quote Post
Serg_
post 16.07.2004, 11:10:28
Post #2





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 10.11.2003

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


Zmieniasz tekst w divie, a nie tworzysz nowego inputa, do tego sluzy metoda createElement().
Kod
function addInput(id)
{
    var oDiv = document.getElementById(id);
    var oInput = document.createElement("<input type='file'>");
    oDiv.appendChild(oInput);
    return;
}

a poziej
  1. <div id='fragment'>
  2. <a href="javascript:addInput('fragment')">Tutaj nowe inputy:</a>
  3.  
  4. </div>


Tym samym sposobem mozna tworzyc dowolnego inputa, tabele, elementy tabeli itp.
O to chodzilo?

Pozdrowka

Serg
Go to the top of the page
+Quote Post
przemoos
post 16.07.2004, 12:48:33
Post #3





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 6.04.2002
Skąd: Lubaczow

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


No, prawie.. smile.gif

Twoja funkcja podczas tworzenia nowego elementu zwracała błąd (przeglądarka Firefox).

Dzięki Tobie jednak napisałem to o co mi chodziło, dzięki za nakierowanie smile.gif
Poniżej cały kod, który działa:

Kod
var countInputs = 0;
function addInput(id)
{
    var oDiv = document.getElementById(id);
    var inputElement = document.createElement("input");
    inputElement.setAttribute("name","userfile_"+countInputs);
    inputElement.setAttribute("type","file");
    oDiv.appendChild(inputElement);
    countInputs++;
}


i do tego html:

  1. <div id="inputLayer"></div>
  2. <a href="javascript:addInput('inputLayer')">Dodaj input</a>
Go to the top of the page
+Quote Post
Serg_
post 16.07.2004, 13:21:12
Post #4





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 10.11.2003

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


A tak, zapomnialem dodac, ze IE.
Standardowo jako parametr metody createElement() podaje sie "input", "td", "tr" itp. A parametry obiektu definiuje sie osobno.
IE jest w tym temacie bardziej tolerancyjny i troszke rozleniwia winksmiley.jpg

Pozdrawiam

Serg

Ten post edytował Serg_ 16.07.2004, 13:21:47
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: 12.06.2025 - 23:48