tags:

views:

42

answers:

5

I have a form where a user may purchase three levels of tickets. With each level, the user chooses a quantity by entering the number of tickets into a plain text input field. When the user leaves that field by clicking on another or tabbing to another, I would like a number of new form fields to be created just below that line, the number derived from the number entered into the quantity field for that specific ticket level. Obviously, it the user decreases the quantity, the input fields should also decrease.

I was looking at the jquery.blur function but apparently it is not reliable across all browsers. Anyone have any suggestions as to how I can achieve this?

Dave

A: 

You can use the jQuery change method. Something like this should work - though you will have to modify it for your purposes.

jQuery("input[@type='text']").change( function() {
   var new_inputs = '';

   //get value for input
   var new_val = this.val();
   for(i=0; i<=new_val; i++)
   {
      new_inputs += "<input type=\"text\" name=\"new-input-"+i+"\" />";
   }// for

   //replace your container with the new inputs - there are many methods of doing this
   jQuery('#my_input_container').html(new_inputs);
});
jeremysawesome
A: 

If you don't want to use .blur() function, you can use a different way:

$('input#input1').keyup(function() {

    var input1 = $('#input1').val();
    $('#input2').val( input1 - 2  );  // Use any equation here

});

jsFiddle

NAVEED
A: 

jQuery

$(function() {

    var input = $('<input type="text" />');
    var newFields = $('');

    $('#qty').bind('blur keyup change', function() {
        var n = this.value || 0;
        if (n+1) {
            if (n > newFields.length) {
                addFields(n);
            } else {
                removeFields(n);
            }
        }
    });

    function addFields(n) {
        for (i = newFields.length; i < n; i++) {
            var newInput = input.clone();
            newFields = newFields.add(newInput);
            newInput.appendTo('#newFields');
        }
    }

    function removeFields(n) {
        var removeField = newFields.slice(n).remove();
        newFields = newFields.not(removeField);
    }
});​

html

<label>Quantity</label><input type="text" id="qty" name="qty" />

<div id="newFields"></div>

crazy demo

Reigel
This seems like it should work well. Thank you. How would I add an id/name to each of these new input fields and then a <br /> or something like that to put all the new inputs on their own line. As far the naming, what I am looking for is the first input being named delegate1 ( <input type="text" name="delegate1" id="delegate1" /> ) and then each subsequent input being incremented by one ( delegagte2, delegate3 etc). Obviouslu when one or more is removed, the names must be decremented to match as well.
Dave
Another thing I thought of: in this script you are cloning the input. If we have more than one inpuyt, isn't it going to duplicate all the input field?
Dave
A: 

OK, I have moved to this point:

<script type="text/javascript">

$(function() {

var input = $('<input type="text" name="delegate" id="delegate" />');
var newFields = $('');

$('#qty').bind('blur keyup change', function() {
    var n = this.value || 0;
    if (n+1) {
        if (n > newFields.length) {
            addFields(n);
        } else {
            removeFields(n);
        }
    }
});

   function addFields(n) {
    for (i = newFields.length; i < n; i++) {

            var newInput = input.clone();
            $(newInput).attr({'name': 'delegate' + (i + 1), 'id': 'delegate' + (i + 1)});
            newFields = newFields.add(newInput);
            newInput.appendTo('#newFields');

            $("<br class='delegate' />").appendTo('#newFields');

    }
}

function removeFields(n) {
    var removeField = newFields.slice(n).remove();
    newFields = newFields.not(removeField);
}

});​

This works fine but I still need to have a label for each new field and seem to be brain dead. I have tried a number of ways to do this with not much success. Can anyone suggest the proper way to add the label in? Also, if you see things wrong with what I am doing here, please suggest a better way so I can learn.

Dave

Dave
Well I have discovered that this is not quite working. I still need to be able to add a label for each input field but I have also figured out that when I change the quantity to a lower number, the script is removing the input fields but not the break tags I have inserted. So, while I am sort of in the right direction, I think I have created a very kludgy script. I need help setting this straight. I need to be able to add or remove input fields, with labels, hopefully with the labels on their own lines just above the input fields ans have these in a vertical orientation. I appreciate the help.
Dave
A: 

OK, I have this mostly working like so:

<script type="text/javascript">

$(function() {

var input = $('<input type="text" name="delegate" id="delegate" />');
var newFields = $(''); 
 $('#bv_qty').bind('blur keyup change', function() {
        var n = this.value || 0;
        if (n+1) {
            if (n > newFields.length) {
                addFields(n);
            } else {
                removeFields(n);
            }
        }
    });
function addFields(n) {
        for (i = newFields.length; i < n; i++) {

            var newInput = input.clone();
            $(newInput).attr({'name': 'delegate' + (i + 1), 'id': 'delegate' + (i + 1)});
            newFields = newFields.add(newInput);
            newInput.appendTo('#memtix');

            $("<br class='delegate' />").appendTo('#memtix');

    }
}

function removeFields(n) {
    var removeField = newFields.slice(n).remove();
    newFields = newFields.not(removeField);
}

I want to add spry validation to each of the outputted text input fields so that they look like this:

<span id="sprytextfield1">
  <input type="text" name="delegate1" id="delegate1" />
  <span class="textfieldRequiredMsg">Please add ticket holder name</span></span>

The number after sprytextfield (1) needs to increment for each outputted field. (the delegate number is already incremented).

I can't quite get how to do it. Any help is appreciated.

Dave

Dave