login  Naam:   Wachtwoord: 
Registreer je!
 Forum

Totale waarde van een array (checkboxes)

Offline Diko - 26/07/2008 12:08 (laatste wijziging 26/07/2008 12:09)
Avatar van DikoPHP interesse Ik probeer de totale waarde van een array checkboxen te krijgen en in een ander veld in te vullen

Ik heb nu het volgende stukje wat absoluut niet werkt:
  1. function calc_rights() {
  2. sum = 0;
  3. for (i=1; i<=document.customers.rights.length; i++)
  4. {
  5. sum = sum + document.customers.rights[i];
  6. }
  7. document.customers.customer_rights.value = sum;
  8. }
  9.  
  10. document.customers.rights[].attachEvent('onChange', 'calc_rights');


Ik heb al even in de code library gezocht hier, maar niets bruikbaars gevonden.

Kan iemand mij hierbij helpen? 

5 antwoorden

Gesponsorde links
Offline CDNC - 26/07/2008 12:30
Avatar van CDNC PHP ver gevorderde document.customers.rights[i] opzich is een array met eigenschappen van die checkbox. (als oik het goed heb, is het te minste een checkbox?)
  1. function calc_rights() {
  2. sum = 0;
  3. for (i=1; i<=document.customers.rights.length; i++)
  4. {
  5. sum = sum + (document.customers.rights[i].checked);
  6. }
  7. document.customers.customer_rights.value = sum;
  8. }
  9.  
  10. document.customers.rights[].attachEvent('onChange', 'calc_rights');
Offline Diko - 26/07/2008 13:25
Avatar van Diko PHP interesse Het is een checkbox idd, maar jouw oplossing werkt helaas niet.

Ik ga nog even verder stoeien 
Offline CDNC - 26/07/2008 22:51
Avatar van CDNC PHP ver gevorderde je zou via een alert een kunnen tonen wat voor iets (document.customers.rights[i].checked)) je terug geeft normaal true/false => 1/0.

Moet i normaal ook niet beginne bij 0?
Offline Diko - 13/08/2008 16:38 (laatste wijziging 13/08/2008 16:46)
Avatar van Diko PHP interesse k heb nu hetvolgende javascript:
  1. function chkTotal(){
  2. var totaal=0;
  3. var frm = document.customers;
  4. var frmCheckboxes = frm.chk;
  5. for(i=0;i<frmCheckboxes.length;i++){
  6. if(frmCheckboxes[i].checked) totaal+=(frmCheckboxes[i].value*1);
  7. }
  8. document.customers.total.value=totaal;
  9. }
  10. window.onload=function(){
  11. chkTotal();
  12. }


waarbij ik de melding krijg dat .length geen object is.

Formuliertje ziet er nu als volgt uit:
  1. <form name="customers" method="post">
  2. <input type="checkbox" name="chk[]" value="1" onclick="chkTotal();">
  3. <input type="checkbox" name="chk[]" value="2" onclick="chkTotal();">
  4. <input type="checkbox" name="chk[]" value="4" onclick="chkTotal();">
  5. <input type="checkbox" name="chk[]" value="8" onclick="chkTotal();">
  6. <input type="checkbox" name="chk[]" value="16" onclick="chkTotal();">
  7. <input type="text" id="total" name="total" value="7" maxlength="3">
  8. </form>
Offline BramBo - 19/08/2008 12:46
Avatar van BramBo JS gevorderde
  1. var frm = null
  2.  
  3. Object.prototype.sum = function() {
  4. var total = 0;
  5.  
  6. for(var i=0; i<this.length;i++) {
  7. if(this[i].checked==null || this[i].checked) total += parseInt(this[i].value)
  8. }
  9. return total;
  10. }
  11.  
  12. function chkTotal() {
  13. if(frm == null) frm = document.forms['customers'];
  14.  
  15. frm.elements['total'].value = frm.elements['chk'].sum();
  16. }
Gesponsorde links
Dit onderwerp is gesloten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.189s