views:

440

answers:

4

I have a select field in a form that allows you to select multiple options.

I need to create a new input textbox for every number of options selected, automatically.

For example, if you select on option in the select box, one input will appear below it. If you select 5, 5 inputs will appear below it.

I'm not sure if this can be done in just straight Javascript, or if I'll need to use jQuery or some other form of AJAX to get this done, but I'm not really sure how to approach this (I'm relatively new to using AJAX)

+1  A: 

Just some quick thoughts.. Add a DIV under the Select Element.

using jQuery (or javascript) you can add a textbox for each select item add a textbox to the div. Something like:

var textboxString;
$("#SelectElement option:selected").each(function () {
                textboxString += "<input type='textbox' /><br>" // Add to this string as needed (Labels, css class, etx).
              });
$("#divElement").html(textboxString)
WorthiGe
Hmm I'm having a hard time getting this to work, is there something else I'm forgetting to add here?
Dave Hunt
A: 

With html like this:

<select id="number_of_inputs"> 
  <option>1</option> 
  <option>2</option> 
  <option>3</option> 
  <option>3</option> 
  <option>5</option> 
</select> 

<div id="inputs"></div>

You can use this jQuery:

$("#number_of_inputs").change(function() {
  var num = parseInt($(this).val());

  $("#inputs").html("");
  for(var i = 0; i < num; i++) {
    $("#inputs").append("<input type='text' /><br />");
  }
});

I actually like gw's JS better but here's another way to do it.

Andy Gaskell
+1  A: 

Well, jQuery was made just for this type of thing - i recommend using it to save yourself headaches.

My take on it.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html lang="en">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">

$(function()
{
  $('#test')
    .after( '<div id="option-inputs"></div>' )
    .find( 'option' ).each( function()
    {
      var $option = $(this);
      $option
        .data( '$input', $( '<span>' + $option.text() + ': </span><input><br>').appendTo( '#option-inputs' ).hide() )
        .bind( 'toggle-input', function()
        {
          var $input  = $option.data( '$input' );
          if ( $option[0].selected )
          {
            $input.show();
          } else {
            $input.hide();
          }      
        })
        .bind( 'click', function()
        {
          $(this).siblings().andSelf().trigger( 'toggle-input' );
        })
        .trigger( 'toggle-input' )
      ;
    })
  ;
});


</script>

</head>
<body>

<select id="test" multiple="multiple">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

</body>
</html>
Peter Bailey
I can't seem to get this to work either... is there something I'm forgetting? I've included the jQuery library..
Dave Hunt
Changed example to full HTML page
Peter Bailey
Okay great, not sure what was wrong before but I got it working. I don't know jQuery so can you help me with one last tweak. I need to be able to limit which options selected create new boxes.So, if an option of value -1 or 0 is selected, a new textbox is not created. Anything else does.Thanks for your help!
Dave Hunt
Also is there any way to have other select forms trigger this to one to update?In regular javascript you'd use onChange="" and a function to update it.The reason for this is that the select table is dynamically populated, based on the input of the previous select table. If you go back and make changes to the previous select table, it doesn't update the jQuery, the previous inputs remain.
Dave Hunt
Yes, but you might be best served by asking that in a new question.
Peter Bailey
A: 

Hi, i have to use code abode but it is not work:

 <select name="field_soure_default[value]" class="form-select" id="edit-field-soure-default-value" >

- None - choloco myamilo yahoo other >

and one text box:

<input type="text" name="field_source[0][value]" id="edit-field-source-0-value" size="60" value="" class="form-text required text" />

i have to change code abode:

$("#edit-field-soure-default-value").change(function() {

var num = parseInt($(this).val());

$("edit-field-source-0-value").html(""); for(var i = 0; i < num; i++) { $("edit-field-source-0-value").append("
"); } }); but it's not works.

and i am to change : $(function() { $('#edit-field-soure-default-value') .after( '' ) .find( 'option' ).each( function() { var $option = $(this); $option .data( '$input', $( '' + $option.text() + ':
').appendTo( '#edit-field-source-0-value' ).hide() ) .bind( 'toggle-input', function() { var $option = $(this); var $input = $option.data( '$input' ); if ( $option[0].selected ) { $input.show(); } else { $input.hide(); }
}) .bind( 'click', function() { $(this).siblings().andSelf().trigger( 'toggle-input' ); }) .trigger( 'toggle-input' ) ; }) ; }); and it's not work.

Please help!