- using jQuery to copy column specific form values
- <script src="../Scripts/jquery-1.7.2.min.js"></script>
- <script>
- $(document).ready(function(){
- $("input#same").click(function()
- {
- if ($("input#same").is(':checked'))
- {
- // Checked, copy values
- $("input#qty").val($("input#same").val());
- }
- else
- {
- // Clear on uncheck
- $("input#quantity").val("");
- }
- });
- });
- </script>
- while( $row = mysql_fetch_array($histresult) )
- {
- echo '<tr height = "50px">';
- echo '<td>'.$product_id.'</td>';
- echo '<td>'.$suggested_quantity.'<input id="same" name="same" type="checkbox" value ="'.$suggested_quantity.'"/> </td>';
- echo '<td><input name="qty" type="text"size="4" maxlength="4"></td>';
- ///Other form elements go here, as well as an Add to Cart Button
- }
- <table>
- <tr>
- <td>
- 100 <input id="check-1" name="same" type="checkbox" value ="100"/>
- <input id="qty-1" name="qty" type="text"size="4" maxlength="4">
- </td>
- <td>
- 100 <input id="check-2" name="same" type="checkbox" value ="100"/>
- <input id="qty-2" name="qty" type="text"size="4" maxlength="4">
- </td>
- <td>
- 100 <input id="check-3" name="same" type="checkbox" value ="100"/>
- <input id="qty-3" name="qty" type="text"size="4" maxlength="4">
- </td>
- </tr>
- </table>
- // Bind click event to ALL checkboxes
- $("#same-*").live("click", function(e) {
- // Only change it if box is checked
- if( $(this).is(":checked") )
- {
- // Get suggested value
- suggested_val = $(this).val();
- // Place in next element (textbox)
- $(this).next().val(suggested_val);
- }
- )};
- <td>'.$suggested_quantity.'<input id="same-' . $row->id . '" name="same" type="checkbox" value ="'.$suggested_quantity.'"/> </td>
- <script>
- $(document).ready(function(){
- $("input.same").click(function()
- {
- if ($(this).is(':checked'))
- {
- // Checked, copy values
- var temp = $(this).attr("title");
- $("input#qty"+temp).val($("input#same"+temp).val());
- }
- else
- {
- // Clear on uncheck
- $("input#qty"+temp).val("");
- }
- });
- });
- </script>
- $i=0;
- while( $row = mysql_fetch_array($histresult) )
- {
- echo '<tr height = "50px">';
- echo '<td>'.$product_id.'</td>';
- echo '<td>'.$suggested_quantity.'<input class="same" id="same'.$i.'" title="'.$i.'" name="same'.$i.'" type="checkbox" value ="'.$suggested_quantity.'"/> </td>';
- echo '<td><input class="qty" name="qty'.$i.'" id="qty'.$i.'" type="text"size="4" maxlength="4"></td>';
- ///Other form elements go here, as well as an Add to Cart Button
- $i++;
- }
- <form id="Order">
- ...
- </form>
- while($row=mysql_fetch_array($histresult))
- {
- echo '<tr height = "50px">';
- echo '<td>'.$product_id.'</td>';
- echo '<td><label>'.$suggested_quantity.'<label><input type="checkbox" class="Same"/> </td>';
- echo '<td><input name="qty" id="qty_'.$product_id.'" type="text"size="4" maxlength="4"></td>';
- ///Other form elements go here, as well as an Add to Cart Button
- }
- <script src="../Scripts/jquery-1.7.2.min.js"></script>
- <script>
- jQuery(document).ready(function(){
- 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
- var Target = jQuery(Event.target); //This is the html element that got clicked on
- if(Target.is("input:checkbox.Same")) //Make sure it's a checkbox that suggests quantity
- {
- 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)
- 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
- if(Target.is(":checked"))
- {
- Text.val(Suggested_quantity);
- }
- else
- {
- Text.val("");
- }
- });
- });
- </script>