在codeigniter中,当我在文本字段中添加“ 1”时,应该仅显示1个文本字段,如果选择2,则显示2个文本字段,否则应将其隐藏
<div class="col-md-6" id="rifa_winners" >
<label for="winners" class="col-md-3 control-label winners" onchange="myFunction()">No Of Winners</label>
<input type="number" name="winners" value="<?php echo $this->input->post('winners'); ?>"
class="form-control winners" id="winners" data-msg="Please enter winners."/>
</div>
<span id="ouput">
<input type="text" name="">
</span>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
答案 0 :(得分:0)
您的出版物还有很多疑问,但是我将尽力为您提供可以根据您的要求进行修改的功能代码。
HTML代码 变化: 1. onchange方法应放置在输入(更改的元素)中,而不是标签文本中。 2.我更改了函数名称,只是为了更具描述性。 3.在输入中向函数传递参数以获取值。
<div class="col-md-6" id="rifa_winners">
<label for="winners" class="col-md-3 control-label winners">No Of Winners</label>
<input type="number" name="winners" value="<?php echo $this->input->post('winners'); ?>" class="form-control winners" id="winners" data-msg="Please enter winners." onchange="setNumberWinners(this.value)"/>
</div>
<div id="output"></div>
Javacript: 该功能基于this SO响应。
function setNumberWinners(val){
// Container <div> where dynamic content will be placed
var container = document.getElementById("output");
// Clear previous contents of the container
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (i=0;i<val;i++){
// Append a node with text Winner X
container.appendChild(document.createTextNode("Winner " + (i+1)));
// Create an <input> element, set its type and name attributes
var input = document.createElement("input");
input.type = "text";
input.name = "winner[]";
container.appendChild(input);
// Append a line break
container.appendChild(document.createElement("br"));
}
}
所有动态创建的输入(winner [])具有相同的名称属性,因此您可以将其作为数组从CI控制器中拾取。 PHP示例
foreach ($this->input->post('winner') as $winner){
// Do whatever you need with the $winner
}
如果您还有其他问题要问我们,我希望这会有所帮助!