Dynamic Fields in a Form

Does anyone know how can I make a dropdown with numbers that display a pair of inputs based on that number?
Say, you want to return 3 items. Selecting number 3 from the dropdown will display 3 pairs of inputs like so:
Product 1 code:
Product 1 name:
Product 2 code:
Product 2 name:
Product 3 code:
Product 3 name: