Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [HTML][JavaScript]dynamiczne dodanie pola formularza - input type=text
Forum PHP.pl > Forum > Przedszkole
kleszcz12
Witam, jak to przerobić:

Kod
var LastInput=0, EmptyInput=0;

function AddFile()
{
    var tmp, tmp2, files, new_input;
    if(this.name.indexOf("file_")==0) { inputid = this.name.replace("file_", ""); tmp = this; }
    else { inputid = 0; tmp = document.getElementsByName("file_0")[0]; }
    if(tmp && tmp.value=='' && inputid!=EmptyInput)
    {
        tmp2 = document.getElementsByName("file_"+EmptyInput)[0];
        files = document.getElementById("files");
        //alert(tmp2);
        if(files && tmp2) { files.replaceChild(tmp, tmp2); }
        if(LastInput!=EmptyInput)
       {
           tmp2 = document.getElementsByName("file_"+LastInput)[0];
           if(tmp2)
           {
                   tmp2.setAttribute("name", "file_"+EmptyInput);
                   if(LastInput==inputid) inputid = EmptyInput;
           }
        }
        EmptyInput = inputid;
        --LastInput;
    }
    else if(tmp && tmp.value!='' && inputid==EmptyInput)
    {
        ++LastInput;
        EmptyInput = LastInput;
        new_input = document.createElement("input");
       new_input.setAttribute("type", "file");
       new_input.setAttribute("name", "file_"+LastInput);
       new_input.className = "textbox";
       new_input.onchange = AddFile;
       new_input.style.width = "400";
       new_input.style.margin = "2px";
       new_input.style.display = "block";
       files = document.getElementById("files");
       if(files) files.appendChild(new_input);
    }
}



formularz:

  1. <form method="post" enctype="multipart/form-data" onsubmit="document.getElementById('uploading').style.display='';">
  2. <input type="hidden" name="action" value="upload">
  3. <input type="hidden" name="dir" value="">
  4. <div id="files">
  5. <input type="file" name="file_0" class="textbox" onchange="AddFile();" style="width:400;margin:2px;display:block;">
  6. </div>
  7. <input type="submit" value="Załaduj" class="button"> <input type="button" value="Zamknij" class="button" onClick="window.close();">
  8. </form>



Aby zamiast uploadu było pole tekstowe (input type=text), próbowałem podmienić w samym formularzu ale wiele to nie pomaga, a javascript'a nie znam kompletnie...prosze o pomoc.

Pozdrawiam
nexis
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="pl" xml:lang="pl">
  3. <title>Test</title>
  4. <script type="text/javascript">
  5. function funkcja () {
  6. var div = document.getElementById("identyfikator");
  7. div.innerHTML += '<input type="text" /><br /><br />';
  8. }
  9. </script>
  10. </head>
  11.  
  12. <body xml:lang="pl">
  13. <div id="nawigacja"><a onclick="funkcja()">dodaj element "<code>&lt;input type=&quot;text&quot; /&gt;</code>"</a></div>
  14. <div id="identyfikator"></div>
  15. </body>
  16. </html>
kleszcz12
Nie działa...
erix
A może trochę więcej szczegółów, niż "nie działa"? Jakieś błędy, czy co...? Bądź precyzyjny, to szybciej dotrzesz do rozwiązania i inni będą mogli Ci pomóc w krótszym czasie.
Shili
A to dziwne, u mnie ów kod działa bez żadnego problemu.
Napisz jak go wplotłeś w swój i również przychylam się do opisania błędów, które wyrzuca konsola, jeśli je wyrzuca.
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.