简体   繁体   English

如何在while循环内添加同一输入框的更多行

[英]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);
    }
  });

});

See JSFiddle for example in action. 有关实际操作,请参见JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 while 循环中添加 select2 动态 select 框? - How can I add select2 dynamic select box inside a while loop? 如何在带有选项卡的div框内添加更多div? - How to add more divs inside a box div with tabs? 如何将属性添加到div内部的输入框中 - How to add an attribute to an input box that is present inside of a div 如何在使用添加更多功能时删除行 - How to remove the rows while using add more functionality 如何使用 jQuery 在按钮单击时添加具有相同 ID 的两行或多行 - How to add with jQuery two or more rows with same ID on button click jQuery在td内添加一个文本框,如果第二次在同一单元格内发生click事件,则阻止添加更多文本框 - jQuery add one text box inside a td and prevent adding more if click event occured second time inside the same cell 如何在表格中添加2行或更多行 - How to add 2 or more rows in table 如何在循环JSON的同时使更有效的js for php在循环内 - how to make more efficient js for loop inside php while loop JSON 如何在javascript或jquery中添加两个或两个以上的输入框值 - how to add tow or more than two input box values in javascript or jquery 如果同一类有多个输入,如何使用jquery“ append”在“ focus”输入之后添加div? - How to use jquery 'append' to add div after an 'focus'ed input, if there are more than one input with same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM