Thread JS: Frage zur Parameterübergabe mit JSON
(51 answers)
Opened by Kuerbis at 2011-08-26 19:59
So schaut der letzte Entwurf aus. Getestet ist er nur mit Firefox, da ich versuchen werden, das in jquery zu schreiben.
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 <!DOCTYPE HTML> <html> <head> <title>Eingabe</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <script type="text/javascript"> function remove_row( tr_id ) { var this_tr = document.getElementById( tr_id ); var tbody = this_tr.parentNode; tbody.removeChild( this_tr ); } function add_row() { var data = { menge : { type : "number", pattern : "\\d+", title : "'Menge' must only contain numbers", size : 8, maxlength : 80, required : "required", autocomplete : "off" }, bezeichnung : { type : "text", size : 40, maxlength : 80, required : "required", autocomplete : "off" }, euro_stueck : { type : "text", pattern : "\\d+(\\,\\d{1,2})?", title : "format: 0,00", size : 12, maxlength : 80, required : "required", autocomplete : "off" }, count : { type : "hidden" }, remove : { type : "button", value : ' - ' } }; var table = document.getElementById( 'product' ); var rows = table.getElementsByTagName( 'tr' ).length; if ( rows > 999 ) { alert( 'Nur 999 Zeilen erlaubt' ); return; } var tr = table.insertRow( rows ); tr.id = 'tr_' + rows; var rr = function() { remove_row( tr.id ); }; for ( var name in data ) { if ( data.hasOwnProperty( name ) ) { var td = document.createElement( 'td' ); var input = document.createElement( 'input' ); for ( var attr in data[name] ) { if ( data[name].hasOwnProperty( attr ) ) { input[attr] = data[name][attr]; } } if ( name == 'count' ) { input.name = name; input.value = rows; } else if ( name == 'remove' ) { input.onclick = rr; input.name = name + '_' + rows; } else { input.name = name + '_' + rows; } td.appendChild( input ); tr.appendChild( td ); } } window.scrollTo( 0, document.body.scrollHeight ); } </script> </head> <body> <form> <table id="product"> <tr id="tr_0"><th>Menge</th><th>Bezeichnung</th><th>Euro/Stück</th></tr> <tr id="tr_1"> <td><input type="number" pattern="\d+" title="'Menge' must only contain numbers" name="menge_1" required size="08" autocomplete="off" /></td> <td><input type="text" name="bezeichnung_1" required size="40" maxlength="80" autocomplete="on" /></td> <td><input type="text" pattern="\d+(\,\d{1,2})?" title="format: 0,00" name="euro_stueck_1" required size="12" autocomplete="off" /></td> <td><input type="hidden" name="count" value="1" /></td> <td><input type="button" name="remove_1" value=" - " onclick="remove_row('tr_1')" /></td> </tr> </table> <br /> <input id="add" type="button" value="Eintrag hinzufügen" onclick="add_row()" /> <br /><br /> <input type="submit" value="OK"/> </form> </body> </html> Parameter abfragen: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 my $eu = new Number::Format( -thousands_sep => '.', -decimal_point => ',', -int_curr_symbol => "\x{20ac}", ); my @count = $self->param( 'count' ); my @rows; for my $i ( sort { $a <=> $b } @count ) { my $euro_stueck = $eu->unformat_number( $self->param( "euro_stueck_$i" ) ); my $menge = $eu->unformat_number( $self->param( "menge_$i" ) ); my $euro_gesamt = $euro_stueck * $menge; $menge = $eu->format_number( $menge ); $euro_stueck = $eu->format_price( $euro_stueck, 2 ); $euro_gesamt = $eu->format_price( $euro_gesamt, 2 ); push @rows, { menge => $menge, bezeichnung => $self->param( "bezeichnung_$i" ), euro_stueck => $euro_stueck, euro_gesamt => $euro_gesamt, }; } Last edited: 2011-09-02 15:19:36 +0200 (CEST) |