要将按钮添加到 keyInfo
的中间,首先需要确定你想要插入按钮的位置。根据你的代码,keyInfo
应该是在处理完 AJAX 请求并解析 JSON 数据后生成的内容。下面是一个简单的方法来将按钮放在 keyInfo
中间。
以下是修改后的代码示例:
$.ajax({
method: "POST",
url: "../d/detail/keyInfo",
data: { custId: custId },
success: function(result) {
var jsonRes = JSON.parse(result);
// 先处理客户信息和贷款信息
for (const item of jsonRes) {
if (item.code === 'customerInfo') {
// 这里处理客户信息展示
}
if (item.code === 'loan') { // 合同展示
// 处理贷款项逻辑(原有逻辑)
// 在这里插入按钮
const buttonContainer = document.createElement('div');
buttonContainer.innerHTML = `
<div class="button-container">
<button class="btn" data-action="action1">按钮1</button>
<button class="btn" data-action="action2">按钮2</button>
<button class="btn" data-action="action3">按钮3</button>
<button class="btn" data-action="action4">按钮4</button>
</div>
`;
// 在某个特定位置插入 buttonContainer,例如在 item.child 的某个地方
document.body.appendChild(buttonContainer); // 或者指定具体容器
buttonContainer.querySelectorAll('.btn').forEach(button => {
button.addEventListener('click', function() {
const action = this.getAttribute('data-action');
switch (action) {
case 'action1':
console.log('按钮1被点击');
break;
case 'action2':
console.log('按钮2被点击');
break;
case 'action3':
console.log('按钮3被点击');
break;
case 'action4':
console.log('按钮4被点击');
break;
default:
break;
}
});
});
for (const loan of item.child) {
const Index = loan.path.split('.')[1];
let contractCode;
me.overDueInfoIndex[loan.path] = Index;
const child_OverduePeriod = loan.child.find(val => val.code === "overduePeriod");
const child_LoanCharacter = loan.child.find(val => val.code === "loanCharacter");
if (child_LoanCharacter && child_LoanCharacter.value === "Y") {
Object.assign(authMapLoan, { totalPaymentAmount: '' });
}
const child_ProductLine = loan.child.find(val => val.code === "productLine");
if (child_ProductLine && !child_ProductLine.ifVirtual) {
Object.assign(authMapLoan, { installmentLoanAmount: '' });
const productLine = child_ProductLine.line;
const index_isRolloverWhite = loan.child.findIndex(val => val.code === 'isRolloverWhite');
loan.child = loan.child.reduce((arr, child) => {
if (child.code == 'overduePeriod') {
if (child.title.includes("(") && child.title.includes(")")) {
child.title = child.title.replace('(', '(').replace(')', ')');
}
}
if (child.code == 'loanStatus' && child_OverduePeriod && child_OverduePeriod.value == '0') {
child.valueMap[child.value] += `(${lang('NO_DEBT')})`;
}
if (child.code === 'contractCode') {
contractCode = child.value;
me.loanPathContractCodes[loan.path] = child.value;
}
if (child.code === 'allowCollNextPayment' && !child.value) return arr;
const DEFAULT_SHOW = !(child.code in authMapLoan);
const AUTH_SHOW = (child.code in authMapLoan) && canI(authMapLoan[child.code]);
if (DEFAULT_SHOW || AUTH_SHOW) {
arr.push(child);
}
return arr;
}, []);
}
}
}
}
me.keyInfo = jsonRes; // 可以直接使用 jsonRes,而不必再次解析 result。
me.form.initForm(jsonRes);
}
});
修改重点:
- 创建并添加按钮信息:在贷款 (
loan
) 的处理逻辑内,我将创建的buttonContainer
放在了适当的位置。 - 避免重复解析:最后,不再对
result
再次进行 JSON 解析,而是直接使用已解析的jsonRes
.
请根据实际 DOM 结构,将 document.body.appendChild(buttonContainer)
替换为合适的容器,以确保按钮信息出现在预期位置。
内容由零声教学AI助手提供,问题来源于学员提问