Sorry if this has been asked before, but I've been digging for a few hours now with little luck.
What I'm trying to accomplish: I wish to have a form that starts by asking the user how many children they need to register (minimum of 1 max of 10). I then need to be able to use this variable to make visible up to 10 entry fields for the children. What I was planning on doing is using the value from the datalist then using an if/else statement to generate the form. (if id = 2 then (form looks like this...) else... What I don't know how to do is capture the choice and then pass it to the if/then statement.
<h2>How many children are you registering?</h2>
<input type="range" value="1" "min=1" max="10" list="number" />
<datalist id="number">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</datalist>
HTML:
<h2>How many children are you registering?</h2>
<input type="range" value="1" "min=1" max="10" list="number" onChange="change(this);" />
<datalist id="number">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</datalist>
<div id="childrens">
</div>
JAVASCRIPT (in < head >):
function change(el) {
var container = document.getElementById('childrens'),
child;
// Clear container
container.innerHTML = '';
// Add number of child inputs
for (var i=0; i<el.value; i++) {
child = document.createElement("input");
child.type = 'text';
container.appendChild( child );
}
}
Use Jquery
Try with this.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h2>How many children are you registering?</h2>
<input type="range" value="1" "min=1" max="10" list="number" id="list" />
<datalist id="number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" >3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</datalist>
<div id="form_input">
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$('#list').change(function(){
var i = $(this).val();
var m = 0;
$('#form_input').html("");
if(i != 0){
$('#form_input').append('<form id="form">');
while(m<i){
$('#form_input').append('<input type="text" id="text_'+m+'" name="text_'+m+'" />'); m++;
}
$('#form_input').append('</form>');
}
});
</script>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.