[英]how to add more rows of same input box inside a while loop
HTML : HTML:
<table>
<tr>
<td>
<input type="text" name="name[]" value=" echo $ail">
<label >Address</label>
<input type="text" name="countryname[]" id='countryname_1' />
<button type="button" class='btn btn-danger delete'>- Delete</button>
<button type="button" class='btn btn-success addmore'>+ Add More</button>
</td>
</tr>
</table>
Js : Js:
<script>
$(".delete").on('click', function() {
$('.case:checkbox:checked').parents("tr").remove();
$('.check_all').prop("checked", false); check();
});
var i = $('table tr').length;
$(".addmore").on('click', function(){
count = $('table tr').length;
var data = "<tr><td><input type='checkbox' class='case'/></td><td><span id='snum" + i + "'>" + count + ".</span></td>"; data += "<td><input class='form-control' type='text' id='countryname_" + i + "' name='countryname[]' required/></td> </tr>"; $('table').append(data); row = i;
});
</script>
I am getting multiple values in <? echo $ail;?>
我在
<? echo $ail;?>
获得多个值<? echo $ail;?>
<? echo $ail;?>
. <? echo $ail;?>
How to give add more button in front of every $ail
value to give a row of a input box? 如何在每个
$ail
值前添加更多按钮以提供一行输入框?
Because I have multiple values of $ail
when I click on one value of Add
button I get incremented on every field of $ail
. 因为我有多个值
$ail
当我点击的一个值Add
按钮,我得到递增的各个领域$ail
。
you can use this example for your problem. 您可以使用此示例解决问题。
js code : js代码:
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
Html code: HTML代码:
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div><input type="text" name="mytext[]"></div>
</div>
The question isn't very clear but presumably <? echo $ail;?>
这个问题不是很清楚,但大概是
<? echo $ail;?>
<? echo $ail;?>
is displaying the content of a PHP array but you want to loop through the array and achieve something similar the functionality presented in the image. <? echo $ail;?>
正在显示PHP数组的内容,但是您希望遍历该数组并实现与图像中所示功能相似的功能。
PHP code: PHP代码:
<?php
// $options starts life as an empty string
$options = "";
// $options becomes a list of options for a <select> control
// it is populated with the values from the $ail array
foreach ($ail as $value) {
$options = "<option value='".$value."'>".$value."</option>";
}
?>
HTML code: HTML代码:
<table>
<tr>
<td>
Ailment:
<select id="ail_select" name="ail_select">
<option value="">--- Select ---</option>
<?php echo $options; ?>
</select>
<button type="button" class='btn btn-success addmore'>Add More</button>
</td>
</tr>
</table>
JS code: JS代码:
$(document).ready(function(){
$(".delete").on("click", function() {
$(".case:checkbox:checked").parents("tr").remove();
$(".check_all").prop("checked", false);
check();
});
var counter = $("table tr").length - 1;
$(".addmore").on("click", function(){
// get the selected ailment
var ail = $("#ail_select").val();
// ail must have a value
if (ail.length > 0) {
counter++;
var data = "<tr><td><input type='checkbox' class='case'/></td><td><span id='snum" + counter + "'>" + ail + ".</span></td>";
data += "<td><input class='form-control' type='text' id='countryname_" + counter + "' name='countryname[]' required/></td>";
data += "</tr>";
$("table").append(data);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.