gpt4 book ai didi

javascript - jQuery 在动态创建的表/单元格中找不到复选框值

转载 作者:行者123 更新时间:2023-12-03 03:52:03 26 4
gpt4 key购买 nike

我试图在单击按钮时从动态创建的表中查找“CheckBox”(0,1,2)的值。复选框位于表格每行的最后一个单元格中。单击复选框时,jQuery 不会给我“值”。这是我的代码:

<body>

<table id="DataTable"
style="position:absolute;left:348px;top:80px;width:350px;height:10px;z-
index:7;text-align:center;"></table>

<input type="submit" id="Button4" name="Clk_In" value="Create Table"
style="position:absolute;left:495px;top:250px;width:96px;height:35px;z-
index:0;">


<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
</script>
<script> // jQuery code below

var count = 0;

// create table heading & rows by clicking button
$(document).on("click","#Button4", function() {

drawHeading();

for( var i = 0; i < 3; i++ ) {
drawRow();
}
});

function drawHeading() {

var row = $("<tr />")
$("#DataTable").append(row);
row.append($("<th>" + "Trans#" + "</th>"));
row.append($("<th>" + "DayOfYear" + "</th>"));
row.append($("<th>" + "Vendor" + "</th>"));
row.append($("<th>" + "Amount&nbsp;($)" + "</th>"));
row.append($("<th>" + "Remove" + "</th>"));

}

function drawRow( ) {

var row = $("<tr />")
$("#DataTable").append(row);
row.append($("<td>" + "cell1" + "</td>"));
row.append($("<td>" + 2 + "</td>"));
row.append($("<td>" + "cell3" + "</td>"));
row.append($("<td>" + 4 + "</td>"));
row.append($("<td/>").append($('<input type="checkbox" name="chkbox2"
value="' + count + '"/>')));

count += 1;
//alert("count: " + count);
}

// find checkBox value *** NOT WORKING should show 0 or 1 or 2 ***
var checkboxValues = [];

$('input[name=chkbox2]:checked').map(function() {
checkboxValues.push($(this).val());
});

alert(checkboxValues[0]);

</script>

任何专家建议将不胜感激。格雷格

最佳答案

您甚至在创建 HTML 之前就检查了复选框值。您可以将代码移动到按钮的单击事件处理程序。

<script>  
// jQuery code below

var count = 0;

// create table heading & rows by clicking button
$(document).on("click","#Button4", function() {

drawHeading();

for( var i = 0; i < 3; i++ ) {
drawRow();
}

// find checkBox value
var checkboxValues = [];

$('input[name=chkbox2]:checked').map(function() {
checkboxValues.push($(this).val());
});

alert(checkboxValues[0]);

});

function drawHeading() {

var row = $("<tr />")
$("#DataTable").append(row);
row.append($("<th>" + "Trans#" + "</th>"));
row.append($("<th>" + "DayOfYear" + "</th>"));
row.append($("<th>" + "Vendor" + "</th>"));
row.append($("<th>" + "Amount&nbsp;($)" + "</th>"));
row.append($("<th>" + "Remove" + "</th>"));

}

function drawRow( ) {

var row = $("<tr />")
$("#DataTable").append(row);
row.append($("<td>" + "cell1" + "</td>"));
row.append($("<td>" + 2 + "</td>"));
row.append($("<td>" + "cell3" + "</td>"));
row.append($("<td>" + 4 + "</td>"));
row.append($("<td/>").append($('<input type="checkbox" name="chkbox2"
value="' + count + '"/>')));

count += 1;
//alert("count: " + count);
}

</script>

如果您想获取选中复选框时的值,您可以监听复选框更改事件。

$('input[name=chkbox2]').change(function() {
//perform your logic
});

关于javascript - jQuery 在动态创建的表/单元格中找不到复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45120579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com