Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
  .percent_product { width: 30px; }
</style>
</head>
<body>
  
  <ul>
     <li>
    <input class="check_product" type="checkbox" checked="checked" name="product_1" />
    <input class="percent_product" type="text" value="20" />&#37;
    <label class="label_product" for="product_1" data-label="Sky Blue">Sky Blue</label>
     </li>  
     <li>
    <input class="check_product" type="checkbox" checked="checked" name="product_2" />
    <input class="percent_product" type="text" value="0" />&#37;
    <label class="label_product" for="product_2" data-label="Yellow">Yellow</label>
     </li>
     <li>
    <input class="check_product" type="checkbox" checked="checked" name="product_3" />
    <input class="percent_product" type="text" value="40" />&#37;
    <label class="label_product" for="product_3" data-label="Green">Green</label>
     </li>
    <li>
    <input class="check_product" type="checkbox" checked="checked" name="product_4" />
    <input class="percent_product" type="text" value="0" />&#37;
    <label class="label_product" for="product_4" data-label="Wood Brown">Wood Brown</label>
     </li> 
  </ul>
  
  <button id="serialize" />Serialize</button>
  
  <div id="debug">
  </div>
  
</body>
</html>
 
// this function should serialize my form
function mySerialize() {
  $('#debug').html( '' );
  
  array_products = new Array();
  
  // for each valid product
  $( '.check_product:checked' ).each(
    function( i, obj ) {
        // index and value
        var num = $(obj).next().val();
        var label = $(obj).next().next().attr( 'data-label' );
    
        // build array
        if( ( num > 0 ) && ( typeof num !== undefined ) ) {
          array_products[ label ] = num;
          $('#debug').append( 'index: ' + label + ", value: " + num + "<br />" );
        }      
     });
  
      // FROM HERE array_products SEEMS TO BE EMPTY!
     
     // serialize array
      var serialized_products = '';
      $(array_products).each( function(i, o) {
        serialized_products = serialized_products + urlencode(i) + '=' + urlencode(o) + '&';
      });
      $('#debug').append( 'serialized: ' + serialized_products + "<br />" );
  
}
// bind event on button click
$(document).ready( function() {
  $('#serialize').click( function() { mySerialize(); } );
});
// function helpers
function urlencode (str) {
        str = (str + '').toString();
        return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
        replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers