1321941
1321941

Reputation: 2170

Change number of form inputs based on select value

I have a form formatted like so: http://jsfiddle.net/E3DfA/

What I am trying to achive is so that when the value of the select changes, the number of form inputs changes.

I have searched around, but found nothing that really makes sense to me, I understand I will need to use ajax, but how would I go about this. A push in the right direction would be much appreciated!

Upvotes: 1

Views: 1102

Answers (2)

Otto Allmendinger
Otto Allmendinger

Reputation: 28268

You don't need to use AJAX. You need to capture the input event on your the select element and then generate the HTML code you want to display.

Have a look at the jQuery library, the documentation contains may examples.

Edit

You were nearly there

$("select").change(function () {
      var str = "";
      var count = Number($("select option:selected").val())
      for (var i = 0; i < count; i++) {
          str += '<div class="oneinput"> <label>' + i
                  + "</label> <input name='" + i + "'></input>"
                  + "</div>";
      }

      $("#text").html(str);
});

You used .each where you needed to use a for-loop. each iterates over arrays. ​

Upvotes: 1

metalfight - user868766
metalfight - user868766

Reputation: 2750

Are you looking for this ?

Its very basic example :

Basic example

Upvotes: 0

Related Questions