gpt4 book ai didi

javascript - 无法对动态生成的表执行单击

转载 作者:行者123 更新时间:2023-12-03 08:07:45 24 4
gpt4 key购买 nike

我正在开发一个网站,并尝试在动态生成的表上实现 onClick 函数。我使用 javascript(testBoot.php) 从 HTML 元素获取值,并将其发送到 pHp (table.php) 以查询数据库基于 testBoot.php 上的 id 并在表中显示结果。现在我想将 onClick 函数添加到动态生成的表格行中,我也尝试了 jQuery 动态绑定(bind),但仍然无法执行相同的操作
testBoot.php

< script type = "text/javascript" >

function showUser() {
var id = document.getElementById("dynamic_data").value;
if (id == "") {
document.getElementById("table-responsive").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("table-responsive").innerHTML = xmlhttp.responseText;

}
};
xmlhttp.open("GET", "table.php?q=" + id, true);
xmlhttp.send();
}
} < /script>
<HTML>

<BODY>
<div class="table-responsive" id="table-responsive"></div>
<select name=city id="dynamic_data" class="form-control" onchange="showUser()"></select>
</BODY>

</HTML>



table.php

< script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" > < /script>
<script>
$('.clickable').on("click", function() {
$('#name').val($(this).children('td:first-child').text());
});
</script >
<body>
<?php $id=$ _GET[ 'q']; require 'data/connection.php'; // manage id echo "<input type = 'Hidden' id = 'testRunID' value = '".$id. "'>"; $sql=mysql_query( ""); if($sql==f alse){ die(mysql_error()); } echo "

<table id = 'myTable'> " ?>

<tr class='clickable'>

<?php echo "
<th>Area Name</th>
<th>Total Test Cases Executed</th>
<th>Passed</th>
<th>Failed</th>
<th>Others (Running/ Blocked/ Time Out)</th></tr>"; while($row=m ysql_fetch_array($sql)) { $testRunID=$ row[ 'areaName']; $totalCount1=$ row[ 'totalCount']; $passCount1=$ row[ 'passStatus']; $failCount1=$ row[ 'failStatus']; $otherCount=$
totalCount1 - ($passCount1 + $failCount1); ?>

<tr class="notfirst">
<?php echo "<td><a href='products.php?testRunID=".$id. "&areaName=" .$testRunID. "'>".$testRunID. "</a></td>"; //echo "<td>".$testRunID. "</td>"; echo "<td>".$totalCount1. "</td>"; echo "<td>".$passCount1. "</td>"; echo "<td>".$failCount1. "</td>"; echo
"<td>".$otherCount. "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($conn); ?>

<label for="name">Created by:</label>
<input id="name" type="text" />

</body>

最佳答案

使用此版本的 jQuery on 方法来绑定(bind)单击事件。这也适用于动态添加的项目。

$(function(){
$(document).on('click','.clickable', function(e) {
var _this =$(this);
alert('clicked');
// Use _this as needed
});
});

关于javascript - 无法对动态生成的表执行单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34300358/

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