Jump to content

Checkboxes and Complex Array


pbrough

Recommended Posts

I am trying to make a series of 5 check boxes generate a set of values (pricing information).  This would be simple if the variables were a fixed cost but they are not.  If you select one item then the others are reduced, but only when combined.  And different combinations give different results for example 1 & 2 and 4 gives one value but 1 & 2 and 5 will give a different result.  These results don't have to be numeric and it would be easier for me to have them text based anyway.  I would be happy to have hidden div's or other solution to allow the visitor to see the effects that they select dynamically.

 

The combinations required will be:

 

1

2

3

- Result 1

 

1, 2

1, 3

2, 3

- Result 2

 

1, 2 & 4

1, 3 & 4

2, 3 & 4

- Result 3

 

1, 2 & 5

1, 3 & 5

2, 3 & 5

- Result 4

 

1, 2, 4 & 5

1, 3, 4 & 5

2, 3, 4 & 5

- Result 5

 

1, 2, 3

- Result 6

 

1, 2, 3 & 4

- Result 7

 

1, 2, 3 & 5

- Result 8

 

1, 2, 3, 4 & 5

- Result 9

 

4 & 5

- Result 10

 

I hope someone can help.

 

Phil

Link to comment
Share on other sites

what about something like this:

<script type="text/javascript">
  var options = {
    '1'         : 'Result 1',
    '2'         : 'Result 1',
    '3'         : 'Result 1',
    '1:2'       : 'Result 2',
    '1:3'       : 'Result 2',
    '2:3'       : 'Result 2',
    '1:2:4'     : 'Result 3',
    '1:3:4'     : 'Result 3',
    '2:3:4'     : 'Result 3',
    '1:2:5'     : 'Result 4',
    '1:3:5'     : 'Result 4',
    '2:3:5'     : 'Result 4',
    '1:2:4:5'   : 'Result 5',
    '1:3:4:5'   : 'Result 5',
    '2:3:4:5'   : 'Result 5',
    '1:2:3'     : 'Result 6',
    '1:2:3:4'   : 'Result 7',
    '1:2:3:5'   : 'Result 8',
    '1:2:3:4:5' : 'Result 9',
    '4:5'       : 'Result 10'
  };
  function findResult ( ) {
    var form = document.getElementById('myForm');
    var checked = [];
    for(var n=0;n < form['foobar'].length;n++){
      if(form['foobar'][n].checked){
        checked.push(form['foobar'][n].value);
      }
    }
    var key = checked.join(':');
    var msg = 'Nothing is checked';
    if(key && options[key]){
      msg = options[key];
    }else if(key){
      msg = 'No match found';
    }
    document.getElementById('result').innerHTML = msg;
  }

  window.onload = function(){
    findResult();
  }
</script>
<form id="myForm">
  <input type="checkbox" name="foobar" value="1" onclick="findResult();" /> 1<br />
  <input type="checkbox" name="foobar" value="2" onclick="findResult();" /> 2<br />
  <input type="checkbox" name="foobar" value="3" onclick="findResult();" /> 3<br />
  <input type="checkbox" name="foobar" value="4" onclick="findResult();" /> 4<br />
  <input type="checkbox" name="foobar" value="5" onclick="findResult();" /> 5<br />
  <span id="result"></span>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.