javascript - Submiting values -


i create form xy results

for example checking 'a' alone may produce result 1, while if both 'a' , 'b' checked answer result 2. d,e,a may give result 3 while b,e,a give result 2. point.

<form action="">     <input type="checkbox" name="options" value="a" />choice a<br />     <input type="checkbox" name="options" value="b" />choice b<br />     <input type="checkbox" name="options" value="c" />choice c<br />     <input type="checkbox" name="options" value="d" />choice d<br />     <input type="checkbox" name="options" value="e" />choice e<br />        <br />     <input type="submit" value="answer"> 

and jquery along these lines

$(':checkbox').click(function () { var value = $(this).val();  if ($(this).val(["a","b","c"]).is(':checked'))     $('.result1').show(value); else     $('.result1').hide(value);  if ($(this).val(["a","d","e"]).is(':checked'))     $('.result2').show(value); else     $('.result2').hide(value); 

this don't work if me great!

so, given a, b , c want something. different a, b , e might else. i've come this:

$(':checkbox').click(function () {     var checkedboxes = $(':checked');     var values = new array();     $.each(checkedboxes, function(index, value) {         var checkboxvalue = $(value).val();         values.push(checkboxvalue);     });      if (containsonly(values, ['a', 'b', 'c'])) {         alert('hi');     }     if (containsonly(values, ['a', 'd', 'e'])) {         alert('bye');     } });  function containsonly(needles, haystack){      if (needles.length !== haystack.length) {         return false;     }     var result = _.intersection(needles, haystack).length === haystack.length;     return result; } 

so, grabs checked checkboxes , grabs values contained within. once has values, compares them using underscorejs's (underscorejs.org) intersection. means can check see if (and if) of values contained within array.

once check complete , if satisfies conditions, something.

also, why underscore js? provides lot of useful linq-like expressions javascript , saves reinventing wheel. worthwhile iterator functions because saves time , effort.

as always, fiddle: http://jsfiddle.net/kylemuir/budra/5/

hope helps


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -