简体   繁体   English

如何使用HTML和JS在foreach循环中为单个按钮创建动态ID

[英]How to create dynamic id to a single button in foreach loop using html & js

I'v to generate different Id's to a Single button which is under for loop. 我要为for循环下的Single按钮生成不同的ID。 I'd created table tbl_tableMaster (id , tblNo ) 我创建了表tbl_tableMaster (id , tblNo )

I'd created a view which displays the tblno on a button by using foreach loop but, what I've to do is: 我通过使用foreach循环创建了一个在按钮上显示tblno的视图,但是,我要做的是:

  1. generate different id's to a button 生成与按钮不同的ID
  2. onClick of any button(btnclick) the value should be display on a label(lblTableNo) 任何按钮的onClick (btnclick)的值应显示在标签上(lblTableNo)

<?php foreach($tables as $row){ ?>
    <button type = "Submit"
      onclick = "changeLabel()"
      id = "btnclick"
      class = "btn btn-primary mt-1"
      value = "<?php echo $row->table_name;?>" > <?php echo $row->table_name;?> </button>
    <input type = "text"
      name = "txtID"
      id = "txtID<?php echo $row->table_id;?>"
      value = "<?php echo $row->table_id;?>"
      hidden >
<?php } ?>

<div class="card-header ">
  <strong>Table No :</strong> <label id="lblTableNo"></label>
</div>

Try this code it works... 试试这个代码,它的工作原理...

<?php $no=1;  foreach($tables as $row){ ?>
        <button type = "Submit"
          onclick = "changeLabel(<?php echo $no; ?>)"
          id = "btnclick<?php echo $no; ?>"
          class = "btn btn-primary mt-1"
          value = "<?php echo $row->table_name;?>" > <?php echo $row->table_name;?> </button>
        <input type = "text"
          name = "txtID"
          id = "txtID<?php echo $row->table_id;?>"
          value = "<?php echo $row->table_id;?>"
          hidden >
    <?php $no++; } ?>

    <script type="text/javascript">


        function changeLabel(id) {

         var loanamount = document.getElementById('btnclick' + id).value;

          document.getElementById('lblTableNo').innerHTML = loanamount;

        };
        </script>

    <div class="card-header ">
      <strong>Table No :</strong> <label id="lblTableNo"></label>
    </div>

You could edit the button onclick to directly change the label, like this : 您可以编辑按钮onclick来直接更改标签,如下所示:

<?php foreach($tables as $row){ ?>
    <button type = "Submit"
      onclick = "document.getElementById('lblTableNo').innerHTML = this.value;"
      id = "btnclick"
      class = "btn btn-primary mt-1"
      value = "<?php echo $row->table_name;?>" > <?php echo $row->table_name;?> </button>
    <input type = "text"
      name = "txtID"
      id = "txtID<?php echo $row->table_id;?>"
      value = "<?php echo $row->table_id;?>"
      hidden >
<?php } ?>

you can do this like <tr class="tr"><td class="transactionCodeData"><?php echo $transaction['transactionKey']; ?></td> 您可以像<tr class="tr"><td class="transactionCodeData"><?php echo $transaction['transactionKey']; ?></td> <tr class="tr"><td class="transactionCodeData"><?php echo $transaction['transactionKey']; ?></td> on one of your table data tag. <tr class="tr"><td class="transactionCodeData"><?php echo $transaction['transactionKey']; ?></td>放在您的表数据标签之一上。 then on your javascript query 然后在你的JavaScript查询

<script type="text/javascript">
        $(document).ready(function(){
            $(".approve").click(function(){
                var $row = $(this).closest('tr'); //for the row
                var $transactionCode = $row.find(".transactionCodeData").text();

                //try if the js script works
                alert($transactionCode);

                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>controller/method",
                    data: { transactionKey: $transactionCode },
                    success: function(data){
                        location.reload(); 
                    }
                });
            });
        });
    </script>

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

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