Thread JS: Frage zur Parameterübergabe mit JSON (51 answers)
Opened by Kuerbis at 2011-08-26 19:59

Kuerbis
 2011-08-28 18:58
#151893 #151893
User since
2011-03-20
936 Artikel
BenutzerIn
[default_avatar]
Code (html): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE HTML>
<html>
<head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <script type="text/javascript">
                function ad_row()
                {
                        var data = {    
                                menge :         { type : 'number' },
                                bezeichnung :   { type : 'text' },
                                euro_stueck :   { type : 'text' },
                                count :         { type : 'hidden' } };
                        var table = document.getElementById( 'product' );
                        var rows = table.getElementsByTagName( 'tr' ).length;
                        var tr = table.insertRow( rows );
                        for ( name in data ) 
                        {       
                                var td = document.createElement( 'td' );
                                var input = document.createElement( 'input' );
                                if ( name == 'count' ) 
                                {       input.name = name;
                                        input.value = rows;
                                }
                                else
                                {
                                        input.name = name + '_' + rows;
                                }
                                for ( attr in data[name] )
                                {
                                        input[attr] = data[name][attr];
                                }
                                td.appendChild( input );
                                tr.appendChild( td );
                        }
                }
                function remove_last_row()
                {
                        var table = document.getElementById( 'product' );
                        var tbody = table.getElementsByTagName( 'tbody' );
                        tbody[0].removeChild( tbody[0].lastChild );     
                }
         </script>
</head>
<body>
<form>
        <table id="product">
                <tr><th>Menge</th><th>Bezeichnung</th><th>Euro/Stück</th></tr>
                <tr>
                        <td><input type="number" name="menge_1" /></td>
                        <td><input type="text" name="bezeichnung_1" /></td>
                        <td><input type="text" name="euro_stueck_1" /></td>
                        <td><input type="hidden" name="count" value="1" /></td>
                </tr>
        </table>
        <br />
        <input type="button" value="Eintrag hinzufügen" onclick="ad_row()" />
        <br />
        <input type="button" value="Letzten Eintrag entfernen" onclick="remove_last_row()" />
        <br /><br />
        <br /><input type="submit" value="OK"/>
</form>
</body>
</html>


Ich habe noch ein paar Fragen zur remove_last_row() Funktion:
Wird tbody immer automatisch erzeugt an kann eine Tabelle immer nur einen tbody haben?
Ist es ok hier einfach tbody[0] schreiben, ohne zu kontrollieren, wieviele es gibt?
Wüdet ihr einen solchen "Letzen Eintrag entfernen" Button als zu umständlich, überflüssig ansehen oder ist es ok?

View full thread JS: Frage zur Parameterübergabe mit JSON