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
   | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
   <title>test d'un radio</title>
	<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
   <style type="text/css">
   
   </style>
</head>
<body>
   <form id="f1" action="" method="post">
      <label for="r1">r1</label><input id="r1" name="r" type="radio" value="1" />
      <label for="r2">r2</label><input id="r2" name="r" type="radio" value="2" />
      <label for="r3">r3</label><input id="r3" name="r" type="radio" value="3" />
   </form>
   <script type="text/javascript">
   var Utils = {
      Evenement: {
         ajouter: function(elm, evt, fn) {
            if (document.addEventListener) {
               if (elm.length === undefined) {
                  elm.addEventListener(evt, function(e) {
                     fn(e)
                  }, false);
               } else {
                  for (var i = 0, imax = elm.length; i < imax; i++) {
                     elm[i].addEventListener(evt, function(e) {
                        fn(e)
                     }, false);
                  }
               }
            } else if (document.attachEvent) {
                if (elm.length === undefined) {
                    elm.attachEvent("on" + evt, fn);
                } else {
                    for (var i = 0, imax = elm.length; i < imax; i++) {
                        elm[i].attachEvent("on" + evt, fn);
                    }
                }
            }
         }
      },
      Filtre: {
         r: function() {
            var r;
            var elms = document.getElementsByName("r");
            for (var i=0,imax=elms.length; i<imax; i++) {
               if (elms[i].checked) {  //----- condition fautive?
                  r = elms[i].value;
               }
            }
            return r;
         }()
      }
   }
   
   Utils.Evenement.ajouter(document, "click", function(e) {
      var elm = e.target || event.srcElement;
      if (elm.name=="r") {
         alert(Utils.Filtre.r);        //----- alerte affichant undefined
         var r;
         var elms = document.getElementsByName("r");
         for (var i=0,imax=elms.length; i<imax; i++) {
            if (elms[i].checked) {
               r = elms[i].value;
            }
         }
         alert(r);                     //----- alerte affichant la valeur du bouton radio
      }
   });
   </script>
</body>
</html> | 
Partager