-1

有人帮我写了一个脚本来总结所有单选按钮的值。但是当我把它放在我的记事本中时,该功能不起作用。它是一个 jquery 函数,但有人知道如何使用 jquery 函数吗?

<html>  
    <head>
        <title>Inboedelwaardemeter</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
                $(function(){
                       $('#show_sum').on('click', function() {
                        alert(sumCheckedRadioButtons());
                    });
               }); 
               function sumCheckedRadioButtons() {
                      var sum = 0;
                      $('input[type=radio]:checked').each(function(i, el) {
                        sum += Number($(el).val());
                      });
                      return sum;
                    };
        </script>
    </head> 
    <body>
        <br>
        <b> Leeftijd hoofdkostwinner:</b>
        <form name="mijnForm1">
        <br>
            <input type="radio" id="getal1" name="leeftijd"
                value="22" checked> 35 jaar en jonger
              <br>    
            <input type="radio" id="getal2" name="leeftijd"
                value="29"> 36 t/m 50 jaar
              <br>
            <input type="radio" id="getal3" name="leeftijd"
                value="38"> 51 jaar en ouder
              <br>
            <br>
        <b> Samenstelling huishouden:</b>
        <br>
        <form name="mijnForm2">
            <input type="radio" id="getal4" name="huishouden"
                value="22" checked> Alleenstaande
              <br>    
            <input type="radio" id="getal5" name="huishouden"
                value="29"> Echtpaar / Samenwonende
              <br>
              <br>  
        <b> Netto maandinkomen hoofdkostwinner</b>
        <br>
        <form name="mijnForm3">
            <input type="radio" id="getal6" name="hoofdkostwinner"
                value="22" checked> Tot en met €1000,-
              <br>    
            <input type="radio" id="getal7" name="hoofdkostwinner"
                value="29"> €1001,- tot en met €2000,-
              <br>
            <input type="radio" id="getal8" name="hoofdkostwinner"
                value="38"> €2001,- tot en met €3000,-
             <br>
            <input type="radio" id="getal9" name="hoofdkostwinner"
                value="38"> €3001,- of hoger
            <br>
            <br>
            <b> Oppervlakte woning</b>
            <br>
        <form name="mijnForm4">
            <input type="radio" id="getal10" name="Oppervlakte"
                value="22" checked> tot en met90m²
              <br>    
            <input type="radio" id="getal1" name="Oppervlakte"
                value="29"> 91m² tot en met 140m²
              <br>
            <input type="radio" id="getal12" name="Oppervlakte"
                value="38"> 141m² tot en met 190m²
             <br>
            <input type="radio" id="getal13" name="Oppervlakte"
                value="38"> 191m² of meer
            <br>
        </form>
        <input type="button" id="show_sum" value="Show Sum" />
    </body>
 </html>

谢谢

4

2 回答 2

0

首先包含js文件

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>  //inside head tag    

和jQuery代码

 <script>
        $(function(){  //code inside this is called whn document is ready
               $('#show_sum').on('click', function() { //this is called when button is clicked
                alert(sumCheckedRadioButtons());
            });
       }); 

       function sumCheckedRadioButtons() {
              var sum = 0;
              $('input[type=radio]:checked').each(function(i, el) {
                sum += Number($(el).val());
              });
              return sum;
            }

    </script>
于 2013-01-31T12:29:09.227 回答
0

你需要添加jquery库,这里是例子

<html>
<head>
  <title>Test</title>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
       $('input[type="radio"]').click(function(){
         var sum = $('input[type=radio]:checked').length;          
         alert('You have checked '+ sum + ' radio buttons');
       });
    });
</script>
</head>
<body>    
    <input class="radio" type="radio" /> one <br>
    <input class="radio" type="radio" /> two <br>
    <input class="radio" type="radio" /> three <br>
    <input class="radio" type="radio" /> four <br>        
</body>
</html>

按照这个 http://jsfiddle.net/xx8Yg/1/

于 2013-01-31T12:41:07.080 回答