0

Hello javascript experts, I’ve been stuck on this code for few days with no idea how to fix it. I have a textfield where user can put in a value. When the value is submitted a number of dropdown lists will be generated by the inputted value. However I'm not too sure what went wrong with the code but it seems there is no response after you submit the value.

Following is my current code:

Javascript:

 <script type="text/javascript">

function getvalue() {
 var number = document.getnumber.input.value;
 document.getElementById('result').value = number; 
}

function generatedropdown() {
 while (i < number)
 {
     document.write
     (
     <select name="list">
     <option>1</option>
     <option>2</option>
     </select>
     );
     i++;
 }
  }

 </script>

HTML:

 <form name="getnumber">
Input number: <input type="text" name="input">
<input type="button" value="Next" onclick="getvalue()">
  </form> 

<form name="showdropdown">
Number entered: <input type="text" id="result" readonly="readonly">
<input type="button" value="Show lists" onclick="generatedropdown()">   
</form>

EDIT: Sorry I'm a bit noobie at Javascript. I have moved function generatedropdown() to the body tags and now I get some feedback after submitting. However I'm still abit confuse in turning the string into a statement.

4

1 回答 1

1

document.write takes a string...meaning you have to put that statement in quotes...and escape your other quotes (i.e. \") or change them to single quotes...but this isn't going to work either because the body is already written - you need to add it to the body object...consider using innerHTML...or document.body.createElement

于 2013-02-14T15:09:22.427 回答