[英]How would I dynamically create input boxes on the fly?
I want to use the value of a HTML dropdown box and create that number of input boxes underneath. 我想使用HTML下拉框的值并在下面创建该数量的输入框。 I'm hoping I can achieve this on the fly.
我希望我能在飞行中实现这一目标。 Also if the value changes it should add or remove appropriately.
此外,如果值更改,则应适当添加或删除。
What programming language would I need to do this in? 我需要使用哪种编程语言? I'm using PHP for the overall website.
我在整个网站上使用PHP。
Here is an example that uses jQuery to achieve your goals: 这是一个使用jQuery实现目标的示例:
Assume you have following html: 假设您有以下html:
<div>
<select id="input_count">
<option value="1">1 input</option>
<option value="2">2 inputs</option>
<option value="3">3 inputs</option>
</select>
<div>
<div id="inputs"> </div>
And this is the js code for your task: 这是你的任务的js代码:
$('#input_count').change(function() {
var selectObj = $(this);
var selectedOption = selectObj.find(":selected");
var selectedValue = selectedOption.val();
var targetDiv = $("#inputs");
targetDiv.html("");
for(var i = 0; i < selectedValue; i++) {
targetDiv.append($("<input />"));
}
});
You can simplify this code as follows: 您可以按如下方式简化此代码:
$('#input_count').change(function() {
var selectedValue = $(this).val();
var targetDiv = $("#inputs").html("");
for(var i = 0; i < selectedValue; i++) {
targetDiv.append($("<input />"));
}
});
Here is a working fiddle example: http://jsfiddle.net/melih/VnRBm/ 这是一个有效的小提琴示例: http : //jsfiddle.net/melih/VnRBm/
You can read more about jQuery: http://jquery.com/ 您可以阅读有关jQuery的更多信息: http : //jquery.com/
I would go for jQuery. 我会去jQuery。
To start with look at change(), empty() and append() 首先看一下change(),empty()和append()
http://api.jquery.com/change/ http://api.jquery.com/change/
Doing it in javascript is quite easy. 在javascript中进行操作非常简单。 Assuming you've got a number and an html element where to insert.
假设您有一个数字和一个html元素插入位置。 You can obtain the parent html element by using
document.getElementById
or other similar methods. 您可以使用
document.getElementById
或其他类似方法获取父html元素。 The method assumes the only children of the parentElement
is going to be these input boxes. 该方法假定
parentElement
的唯一子parentElement
将是这些输入框。 Here's some sample code: 这是一些示例代码:
function addInput = function( number, parentElement ) {
// clear all previous children
parentElement.innerHtml = "";
for (var i = 0; i < number; i++) {
var inputEl = document.createElement('input');
inputEl['type'] = 'text';
// set other styles here
parentElement.appendChild(inputEl);
}
}
for the select change event, look here: javascript select input event 对于选择更改事件,请看此处: javascript选择输入事件
you would most likely use javascript(which is what jquery is), here is an example to show you how it can be done to get you on your way 您很可能会使用javascript(这就是jquery),下面是一个示例,向您展示如何完成此操作以使您步入正轨
<select name="s" onchange="addTxtInputs(this)" onkeyup="addTxtInputs(this)">
<option value="0">Add</option>
<option value="1">1</option>
<option value="3">3</option>
<option value="7">7</option>
</select>
<div id="inputPlaceHolder"></div>
javascript to dynamically create a selected number of inputs on the fly, based on Mutahhir
answer javascript基于
Mutahhir
答案动态创建选定数量的输入
<script>
function addTxtInputs(o){
var n = o.value; // holds the value from the selected option (dropdown)
var p = document.getElementById("inputPlaceHolder"); // this is to get the placeholder element
p.innerHTML = ""; // clears the contents of the place holder each time the select option is chosen.
// loop to create the number of inputs based apon `n`(selected value)
for (var i=0; i < n; i++) {
var odiv = document.createElement("div"); //create a div so each input can have there own line
var inpt = document.createElement("input");
inpt['type'] = "text"; // the input type is text
inpt['id'] = "someInputId_" + i; // set a id for optional reference
inpt['name'] = "someInputName_" + i; // an unique name for each of the inputs
odiv.appendChild(inpt); // append the each input to a div
p.appendChild(odiv); // append the div and inputs to the placeholder (inputPlaceHolder)
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.