简体   繁体   中英

How to add dynamic ids to text boxes?

I'm trying to assign id's to some text fields that's getting created dynamically in a loop.

The div which is in the loop that consists of the text fields.

<!-- DATA ENTERED IN THIS TEXT BOX WILL BE REFLECTED IN ALL THE TEXT BOXES CREATED IN THE LOOP-->
<div class="col-sm">
                <h5><span class="badge badge-dark">Add Quantity:</span></h5>
           <input class="form-control" type="text" name="add0" id="add0" onkeyup="sync()">

           </div>

<!-- SCRIPT FOR REFLECTING DATA -->

    function sync()
        {
         var add0 = document.getElementById('add0');
         var add1 = document.getElementById('add1');
         var add2 = document.getElementById('add2');
         var add3 = document.getElementById('add3');
         var add4 = document.getElementById('add4');
         var add5 = document.getElementById('add5');
         var add6 = document.getElementById('add6');
         var add7 = document.getElementById('add7');
         var add8 = document.getElementById('add8');
         add8.value = add7.value = add6.value = add5.value = add4.value = add3.value = add2.value =  add1.value = add0.value;
        }


<?php 
<!-- GETS AN ARRAY OF DATA SCANNED IN THE PREVIOUS PAGE -->
$strings = explode(PHP_EOL,($_SESSION['grid']));
$sa = [];
foreach ($strings as $s) {
array_push($sa, "'".$s."'");
}   
$d=implode(',', $sa);

<!-- SQL QUERY -->
$sql = "select distinct size from items where main_group IN(select distinct main_group from items where addl_item_code_barcode IN ($d)) ORDER BY size";
$result = pg_query($db, $sql);
?>
<div class="row">

<!-- THIS LOOP WILL CREATE 8 DIV'S BECAUSE THERE ARE 8 SIZES 36-50 -->
<?php  while($res = pg_fetch_assoc($result)){ ?>

    <div class="col-md-2 show-hide">
    <input type="text" value="<?php echo $res['size']; ?> " readonly 
    style="background-color: #F5F5F5;" class="form-control"/>

    <!-- // OUTPUT WILL BE GENERATED HERE -->
    <div id="addition"></div> 

    <select class="form-control">
       <option>25%</option>
       <option>50%</option>
       <option>100%</option>
    </select><br> 
    </div>
</div>
           <br>
           <br>

               <button type="submit" value="submit" class="btn btn-primary"><i class="fas fa-paint-brush"></i>Apply</button>
 </form>
<?php }?>

<!-- The script that I tried for assigning unique id's -->    
$(document).ready(function() {

  for( var i=1; i<9; i++)  //i<9 because that's the maximum number of 
                           // text fields to be created is 8.     
    {    
         $('#addition')
           .append('<input class="form-control" type="text" name="add" id="add'+i+'" />');
    }

The problem is that this script creates a series of text boxes only in the first iteration of the while loop.

Here's how it looks.

Here this output is creating all the 8 text boxes below the 'size' field ie, '36'. How do I get the 2nd text box below 38 and the 3rd below 42 and so on...

Is there a way only create the id's dynamically and append it in the input area, instead of putting the input itself in the loop like I have done?

 var text = ""; for(var i=0;i<11;i++) { text += '<input class="form-control" type="text" name="add" id="add_'+i+'" /></br>'; } $("#addition").html(text);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="addition"></div>

You can set the id within the while-loop, but you have to set an unique id. Maybe you should include the loop-iterator in the id. Try following example:

<?php  while($res = pg_fetch_assoc($result)){ ?>
<div class="col-md-2 show-hide">
<input type="text" id="field<?php echo $res['size']; ?>" value="<?php echo $res['size']; ?> " readonly 
style="background-color: #F5F5F5;" class="form-control"/>

<div id="addition"></div> // OUTPUT WILL BE GENERATED HERE

<select class="form-control">
<option>25%</option>
<option>50%</option>
<option>100%</option>
</select><br> 
</div><?php }?>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM