Hi @jewel,
Here is my test result:
Sample Code
@{
ViewData["Title"] = "How to add html input textbox, label, checkbox to jqery table";
}
<style>
#blogTable {
border-collapse: collapse;
border: 1px solid black;
}
#blogTable th, #blogTable td {
border: 1px solid black;
padding: 8px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function () {
// init data
loadData();
$("#CompanyDrop").change(function () {
var companyid = $("#CompanyDrop").val();
loadData(companyid);
});
$("#productForm").submit(function (e) {
e.preventDefault();
var products = [];
$("#blogTable tbody tr:not(.total-row)").each(function () {
var product = {
productID: $(this).find("td:eq(0)").text(),
productName: $(this).find("td:eq(1)").text(),
purchaseRate: $(this).find("td:eq(2) label").text(),
qty: ($(this).find("td:eq(3) input").val() == "") ? 0 : $(this).find("td:eq(3) input").val(),
totalValue: parseInt($(this).find("td:eq(2) label").text()) * (($(this).find("td:eq(3) input").val() == "") ? 0 : $(this).find("td:eq(3) input").val()),
isChecked: $(this).find("td:eq(5) input").prop("checked")
};
products.push(product);
});
//...[Ajax call remains unchanged]
});
})
function loadData(companyid) {
$.ajax({
url: companyid == undefined ? ('/Purchase/GetProductRecord?companyid=-1') : ('/Purchase/GetProductRecord?companyid=' + companyid),
type: 'Get',
datatype: 'json',
success: function (data) {
$('#blogTable tbody').empty();
$.each(data, function (i, item) {
var rows = "<tr>"
+ "<td>" + item.productID + "</td>"
+ "<td>" + item.productName + "</td>"
+ "<td><label>" + item.purchaseRate + "</label></td>"
+ "<td><input type='number' value='' min='1' onkeypress='return isNumberKey(event)' class='txt_qty' data-rate='" + item.purchaseRate + "'/></td>"
+ "<td><label class='total-value'></label></td>"
+ "<td><input type='checkbox' name='Selectone' class='chk_isChecked' id='" + item.productID + "'/></td>"
+ "</tr>";
$('#blogTable tbody').append(rows);
});
// After loading data, initialize checkbox status based on qty values
$('#blogTable tbody .txt_qty').each(function () {
var qty = $(this).val();
if (qty && parseInt(qty) > 0) {
$(this).closest("tr").find(".chk_isChecked").prop("checked", true);
}
});
bindEvents();
}
});
}
function bindEvents() {
$(".txt_qty").off("input").on("input", function () {
var rate = $(this).data("rate");
var qty = $(this).val();
var total = rate * qty;
$(this).closest("tr").find(".total-value").text(total);
// Check if qty has a value and is greater than 0, then check the checkbox
if (qty && parseInt(qty) > 0) {
$(this).closest("tr").find(".chk_isChecked").prop("checked", true);
} else {
$(this).closest("tr").find(".chk_isChecked").prop("checked", false);
}
calculateTotalValue();
});
$(".chk_isChecked").off("change").on("change", function () {
calculateTotalValue();
});
calculateTotalValue();
}
function calculateTotalValue() {
var totalValue = 0;
$("#blogTable tbody tr:not(.total-row)").each(function () {
if ($(this).find(".chk_isChecked").prop("checked")) {
var value = $(this).find(".total-value").text();
totalValue += (value === "" ? 0 : parseInt(value));
}
});
$(".total-sum").text(totalValue);
}
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
<form id="productForm" method="post" action="/Purchase/SubmitProducts">
<select class="" id="VenderDrop">
<option value="0">---Select Vender Name----</option>
</select>
<select class="" id="CompanyDrop">
<option value="0">---Select Company Name----</option>
<option value="1">---Select Company 1 ----</option>
<option value="2">---Select Company 2 ----</option>
<option value="3">---Select Company 3 ----</option>
<option value="4">---Select Company 4 ----</option>
<option value="5">---Select Company 5 ----</option>
<option value="6">---Select Company 6 ----</option>
<option value="7">---Select Company 7 ----</option>
<option value="8">---Select Company 8 ----</option>
<option value="9">---Select Company 9 ----</option>
</select>
<br /><br />
<table id="blogTable" class="zui-table zui-table-rounded">
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Purchase Rate</th>
<th>Qty</th>
<th>Value</th>
<th>Checkbox</th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr>
<td colspan="4" style="text-align:center">Total Value</td>
<td colspan="2" style="text-align:center" class="total-sum">0</td>
</tr>
</tfoot>
</table>
<br />
<input type="submit" value="Submit" />
</form>
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Jason