Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Survey Form Total Points

    Hi!

    I have a survey form like so:
    Code:
    <tr>
        <td>Registration Process</td>
    	<td><input name="B" type="radio" value="0"></td>
        <td><input name="B" type="radio" value="1"></td>
        <td><input name="B" type="radio" value="2"></td>
        <td><input name="B" type="radio" value="3"></td>
        <td><input name="B" type="radio" value="4"></td>
        <td><input name="B" type="radio" value="5"></td>
      </tr>
      <tr>
        <td>Cost</td>
    	<td><input name="C" type="radio" value="0"></td>
        <td><input name="C" type="radio" value="1"></td>
        <td><input name="C" type="radio" value="2"></td>
        <td><input name="C" type="radio" value="3"></td>
        <td><input name="C" type="radio" value="4"></td>
        <td><input name="C" type="radio" value="5"></td>
      </tr>
    etc... Where a user can rate items from 1-5 (5 being best); the "0" value is a "not applicable" option.

    My Question: How can I display a total score at the end (i.e. 26/30 points)? I have seen this done with shopping carts, etc, but I don't need any currency options, just the ability to add/subtract the integer point values.

    ALSO, it is important that if a user selects a "n/a (0)" for a category, that the total possible points are lowered to reflect that. For example, if there are 3 categories and the selection is as follows:

    1) "n/a" (0)
    2) "n/a" (0)
    3) "4" (4)

    then the score will be 4/5, not 4/15.

    Anyone know how to do this with Javascript? I am not very familiar with JS, so any help is appreciated (I'm more of a server-side guy). Cross-browser is good, but not essential.

    Thanks!

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,504
    Thanks
    3
    Thanked 501 Times in 488 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    
    function Cal(obj){
     var total=0;
     var ticked=0;
     for (var zxc0=1;zxc0<obj['B'].length;zxc0++){
      if (obj['B'][zxc0].checked){
        total+=obj['B'].length-1;
        ticked+=zxc0;
      }
     }
     for (var zxc1=1;zxc1<obj['C'].length;zxc1++){
      if (obj['C'][zxc1].checked){
        total+=obj['C'].length-1;
        ticked+=zxc1;
      }
     }
     for (var zxc2=1;zxc2<obj['D'].length;zxc2++){
      if (obj['D'][zxc2].checked){
        total+=obj['D'].length-1;
        ticked+=zxc2;
      }
     }
     obj['Score'].value='Score: '+ticked+'/'+total;
    }
    //-->
    </script></head>
    
    <body>
    <form >
    <table onclick="Cal(this.parentNode);" >
    <tr>
        <td>Registration Process</td>
    	<td><input name="B" type="radio" ></td>
        <td><input name="B" type="radio" ></td>
        <td><input name="B" type="radio" ></td>
        <td><input name="B" type="radio" ></td>
        <td><input name="B" type="radio" ></td>
        <td><input name="B" type="radio" ></td>
      </tr>
      <tr>
        <td>Cost</td>
    	<td><input name="C" type="radio" ></td>
        <td><input name="C" type="radio" ></td>
        <td><input name="C" type="radio" ></td>
        <td><input name="C" type="radio" ></td>
        <td><input name="C" type="radio" ></td>
        <td><input name="C" type="radio" ></td>
      </tr>
      <tr>
        <td>Cost</td>
    	<td><input name="D" type="radio" ></td>
        <td><input name="D" type="radio" ></td>
        <td><input name="D" type="radio" ></td>
        <td><input name="D" type="radio" ></td>
        <td><input name="D" type="radio" ></td>
        <td><input name="D" type="radio" ></td>
      </tr>
      <tr>
    	<td colspan="6"><input name="Score" type="text" value="0"></td>
      </tr>
    </table>
    </form>
    
    </body>
    
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •