Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 4.27 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using jQuery to copy column specific form values
  2. <script src="../Scripts/jquery-1.7.2.min.js"></script>
  3. <script>
  4. $(document).ready(function(){
  5.     $("input#same").click(function()
  6.     {
  7.         if ($("input#same").is(':checked'))
  8.         {
  9.             // Checked, copy values
  10.             $("input#qty").val($("input#same").val());
  11.         }
  12.         else
  13.         {
  14.             // Clear on uncheck
  15.             $("input#quantity").val("");
  16.  
  17.         }
  18.     });
  19. });
  20. </script>
  21.        
  22. while( $row = mysql_fetch_array($histresult) )
  23. {
  24.     echo '<tr height = "50px">';
  25.  
  26.  
  27.     echo '<td>'.$product_id.'</td>';
  28.     echo '<td>'.$suggested_quantity.'<input id="same" name="same" type="checkbox" value ="'.$suggested_quantity.'"/> </td>';
  29.     echo '<td><input name="qty" type="text"size="4" maxlength="4"></td>';
  30.     ///Other form elements go here, as well as an Add to Cart Button
  31. }
  32.        
  33. <table>
  34.    <tr>
  35.       <td>
  36.          100 <input id="check-1" name="same" type="checkbox" value ="100"/>
  37.              <input id="qty-1" name="qty" type="text"size="4" maxlength="4">
  38.       </td>
  39.       <td>
  40.          100 <input id="check-2" name="same" type="checkbox" value ="100"/>
  41.          <input id="qty-2" name="qty" type="text"size="4" maxlength="4">
  42.       </td>
  43.       <td>
  44.          100 <input id="check-3" name="same" type="checkbox" value ="100"/>
  45.          <input id="qty-3" name="qty" type="text"size="4" maxlength="4">
  46.       </td>
  47.    </tr>
  48. </table>
  49.        
  50. // Bind click event to ALL checkboxes
  51. $("#same-*").live("click", function(e) {
  52.  
  53.    // Only change it if box is checked
  54.    if( $(this).is(":checked") )
  55.    {
  56.       // Get suggested value
  57.       suggested_val = $(this).val();
  58.  
  59.       // Place in next element (textbox)
  60.       $(this).next().val(suggested_val);      
  61.    }
  62. )};
  63.        
  64. <td>'.$suggested_quantity.'<input id="same-' . $row->id . '" name="same" type="checkbox" value ="'.$suggested_quantity.'"/> </td>
  65.        
  66. <script>
  67. $(document).ready(function(){
  68.     $("input.same").click(function()
  69.     {
  70.         if ($(this).is(':checked'))
  71.         {
  72.             // Checked, copy values
  73.             var temp = $(this).attr("title");
  74.             $("input#qty"+temp).val($("input#same"+temp).val());
  75.         }
  76.         else
  77.         {
  78.             // Clear on uncheck
  79.             $("input#qty"+temp).val("");
  80.  
  81.         }
  82.     });
  83. });
  84. </script>
  85.  
  86.  
  87.  
  88. $i=0;
  89. while( $row = mysql_fetch_array($histresult) )
  90. {
  91.     echo '<tr height = "50px">';
  92.  
  93.  
  94.     echo '<td>'.$product_id.'</td>';
  95.     echo '<td>'.$suggested_quantity.'<input class="same" id="same'.$i.'" title="'.$i.'" name="same'.$i.'" type="checkbox" value ="'.$suggested_quantity.'"/> </td>';
  96.     echo '<td><input  class="qty" name="qty'.$i.'" id="qty'.$i.'" type="text"size="4" maxlength="4"></td>';
  97.     ///Other form elements go here, as well as an Add to Cart Button
  98.     $i++;
  99. }
  100.        
  101. <form id="Order">
  102. ...
  103. </form>
  104.        
  105. while($row=mysql_fetch_array($histresult))
  106.         {
  107.             echo '<tr height = "50px">';
  108.  
  109.  
  110.             echo '<td>'.$product_id.'</td>';
  111.             echo '<td><label>'.$suggested_quantity.'<label><input type="checkbox" class="Same"/> </td>';
  112.             echo '<td><input name="qty" id="qty_'.$product_id.'" type="text"size="4" maxlength="4"></td>';
  113.                             ///Other form elements go here, as well as an Add to Cart Button
  114.                    }
  115.        
  116. <script src="../Scripts/jquery-1.7.2.min.js"></script>
  117. <script>
  118.     jQuery(document).ready(function(){
  119.     jQuery("form#Order").click(function(Event){ //One event handler for the form, more efficient that way and you need less html structure to keep track of things
  120.         var Target = jQuery(Event.target); //This is the html element that got clicked on
  121.         if(Target.is("input:checkbox.Same")) //Make sure it's a checkbox that suggests quantity
  122.         {
  123.             var Text = jQuery(Target.closest('tr').children().get(2)).children();   //Get the parent TR tag, get it's third child (td tag containing the text field), get it's child (the text field)
  124.             var Suggested_quantity = Target.prev().html(); //Get the previous sibling which is the label containing the quantity and get it's html content which is the quantity
  125.             if(Target.is(":checked"))
  126.             {
  127.                 Text.val(Suggested_quantity);
  128.             }
  129.             else
  130.             {
  131.                 Text.val("");
  132.             }
  133.     });
  134. });
  135. </script>