javascript - 点击功能在jquery中不起作用
问题描述
在下面的代码中,当有人单击 SELL 按钮(第一个 td)但单击功能不起作用时,我想运行一个函数。它说未定义运行功能。
function runfunction() {
alert("success");
}
sendRequest(); //call function
function sendRequest() {
$.ajax({
type: "POST",
url: '<?php echo site_url("ajax/ajax_buybtc"); ?>',
success: function(result) {
var jsonResult = JSON.parse(result);
$(".tab1 tr.tr1").remove();
jsonResult.forEach(function(data) {
var newTr = ""; // I delete the value to avoid double tr declaration
newTr +=
'<tr class="tr1"> <td onclick="runfunction()" style="text-align:center;"><a><span class="label label-warning">Sell</span></a></td>';
newTr += "<td id='xbtc " + data.xbtc + "'>" + data.xbtc + "</td>";
newTr += "<td id='xbtc " + data.rate + "'>" + data.rate + "</td>";
newTr += "<td id='xbtc " + data.xpkr + "'>" + data.xpkr + "</td>";
newTr += "</tr>";
$(".tab1 > tbody:last-child").append(newTr);
});
setTimeout(function() {
sendRequest(); //this will send request again and again;
}, 4000);
},
});
}
解决方案
您需要更改onclick="runfunction()"
为onclick="runfunction"
. 目前正在发生的事情是runfunction
被执行,并且它的返回值被绑定到onclick
事件。您希望绑定函数本身,而不是其返回值。
推荐阅读
- javascript - 使用更高效的 blob 或 base64 进行上传
- signalr - SignalR Core 授权与刷新令牌
- javascript - 如何将字符串数据:application/octet-stream;base64 转换为数据:image/(png, jpeg...);base64 显示图像
- r - 动态地将数据转换为数据框 - Rstudio
- javascript - Node.js RangeError:超出最大调用堆栈大小
- javascript - JS 承诺:这个承诺是否等同于这个 async/await 版本?
- amazon-web-services - 如何更改亚马逊 s3 存储桶的端点 url 结构?
- reactjs - Laravel CORS 第一篇文章的问题,如何解决?
- c# - 如果我在 C# 中创建了一个 SQL Server 数据库,我在哪里可以检查它?
- python - 在 sqlite 中选择一行