jquery - 如何使用 wordpress 在 DOM 中附加回调函数
问题描述
我在将回调函数显示在 HTML DOM 上时遇到问题。想法是获取基金的 ID,然后使用 ID 获取基金价格和查看历史记录。我得到的是未定义的
查看历史记录应该将用户带到他们在分页表中查看基金价格历史记录的地方。
对于查看历史页面,想法是将 id 传递到 url 并获取想法并在传递到 url 的 id 上查询端点页面。但查看历史记录需要基金 ID、开始日期和结束日期 https://api.premiumpension.com/multichannel/swagger/ui/index#!/Prices/Prices_GetFundPriceByDateRange
我将非常感谢您的帮助谢谢
jQuery(document).ready(function ($) {
jQuery.ajax({
type: 'GET',
url: 'https://api.premiumpension.com/multichannel/api/Prices/GetAllFundNames',
contentType: "application/json",
success: function(resp) {
for (var i = 0; i < resp.result.length; i++) {
// console.log('hello: results', JSON.stringify(resp.result[i].FUND_NAME));
//${resp.result[i].FUND_ID} ${resp.result[i].FUND_NAME}
//callbackfun(resp.result[i].FUND_ID)
callbackfun(resp.result[i].FUND_ID);
$('#pensionfund').append(
`<div col-md-3>
<h5>Price ${i+1}</h5>
<a id='#price${i+1}'>${resp.result[i].FUND_NAME}
<hr>
<p id="priceplace">${callbackfun(resp.result[i].FUND_ID)}</p>
<hr>
<a target="_blank" href="https://p/view-history/${i+1}" rel="noopener noreferrer">View History</a>
</p>
</div>`
);
}
},
error: function(xhr, status, error){
var errorMessage = xhr.status + ': ' + xhr.statusText
alert('Error - ' + errorMessage);
}
});
});
function callbackfun(resp)
{
jQuery.ajax({
type: 'GET',
url: 'https://api.premiumpension.com/multichannel/api/Prices/GetCurrentFundPrice?fundId=' + resp ,
contentType: "application/json",
success: function(resp) {
console.log("This the result: ",resp);
for (var i = 0; i < resp.result.length; i++) {
//console.log("This is the innerLoop", resp.result[i].UnitPrice)
$('#priceplace').append(`<h5> ${resp.result[i].UnitPrice}</h5>`);
//document.getElementById('unitprice').innerText = resp.result[i].UnitPrice;
}
},
});
}
```
解决方案
在下面的代码中,我将每个 div 的值分配给append_data
for 循环,然后将该数据添加到<div id="pensionfund"></div>
using .html()
。另外,我已经id
为所有将显示的 div分配了唯一性,unit-price
因为我们不能有很多 id same name
。工作示例:
// find elements
var append_data = "";
$(document).ready(function($) {
$.ajax({
type: 'GET',
url: 'https://api.premiumpension.com/multichannel/api/Prices/GetAllFundNames',
contentType: "application/json",
success: function(resp) {
for (var i = 0; i < resp.result.length; i++) {
callbackfun(resp.result[i].FUND_ID)
//append data to variable
append_data += "<div col-md-3> <h5>Price '" + i + 1 + "'</h5<p id='#price'" + i + 1 + "'>" + resp.result[i].FUND_NAME + "<hr><p id='priceplace_" + resp.result[i].FUND_ID + "' onclick='callbackfun(" + resp.result[i].FUND_ID + ")'></p><hr><a target='_blank' href='https://p/view-history/" + i + 1 + "' rel='noopener noreferrer'>View History</a></p></div>";
//console.log(resp.result[i].FUND_ID);
}
//put data in div with id pensionfund
$("#pensionfund").html(append_data);
},
error: function(xhr, status, error) {
var errorMessage = xhr.status + ': ' + xhr.statusText
alert('Error - ' + errorMessage);
}
});
});
function callbackfun(resp1) {
$.ajax({
type: 'GET',
url: 'https://api.premiumpension.com/multichannel/api/Prices/GetCurrentFundPrice?fundId=' + resp1,
contentType: "application/json",
success: function(resp) {
//assign price to particular id i.e :priceplace_1,priceplace_11 etc
$('#priceplace_' + resp1).html("<h5> UNIT PRICE : " + resp.result.UnitPrice + "</h5>");
},
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<div id="pensionfund"></div>
推荐阅读
- docker - Docker 构建:未找到匹配的发行版
- javascript - 如何在 electron.js 中的不同模块之间分离 MenuItem 方法的声明和实现
- php - 在 laravel 中使用 php-ffmpeg 转换和保存视频时编码失败
- ios - CIFilter 导致 iPhone 11 和 XR 崩溃
- javascript - 反应输入和状态不能覆盖
- awk - 这个按模式过滤的 awk 有什么问题?
- java - 如何在类路径中定义 lib 版本?
- c# - HttpRequestException - 1&1 Ionos 计划上的 SendGrid 电子邮件 API v3 集成
- json - 如何过滤wso2 sp复杂json处理的查询
- flutter - 引发了另一个异常:'package:flutter/src/widgets/navigator.dart':断言失败:第 3803 行 pos 12:'_history.isNotEmpty':不正确