[英]How to create dynamic id to a single button in foreach loop using html & js
我要為for循環下的Single按鈕生成不同的ID。 我創建了表tbl_tableMaster (id , tblNo )
我通過使用foreach循環創建了一個在按鈕上顯示tblno
的視圖,但是,我要做的是:
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>
試試這個代碼,它的工作原理...
<?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>
您可以編輯按鈕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 } ?>
您可以像<tr class="tr"><td class="transactionCodeData"><?php echo $transaction['transactionKey']; ?></td>
<tr class="tr"><td class="transactionCodeData"><?php echo $transaction['transactionKey']; ?></td>
放在您的表數據標簽之一上。 然后在你的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.