【发布时间】:2019-01-16 12:26:47
【问题描述】:
您能否帮我解决这个问题,因为我只是学习 ajax 并尝试将其纳入我的要求。
我的要求:
当我单击特定图像时,它应该调用同一页面,但会根据调用的参数值加载具有不同值的表数据。因此建议我使用 AJAX,因为它不会重新加载整个页面。
JQUERY
$("#goToCostTypeID").click(function () {
var costType = document.getElementById("costType").value;
if(costType == "Actual"){
costType = "Budget";
document.getElementById("costType").value = "Budget";
} //if actuals ends
if(costType == "Budget"){
costType = "Forecast";
document.getElementById("costType").value = "Forecast";
} //if actuals ends
if(costType == "Forecast"){
costType = "Actual";
document.getElementById("costType").value = "Actual";
} //if actuals ends
var Budget = costType;
$.ajax({
dataType : "html",
url:'my.jsp?productID=6&appID=6&txtHidden=Costs&mode=Edit&costType='+costType,
type:'POST',
contentType :'application/x-www-form-urlencoded; charset=UTF-8',
data:{costType:Budget},
success:function(result){
console.log("YES");
$("#costContent").load('my.jsp?productID=6&appID=6&txtHidden=Costs&mode=Edit&costType='+costType);
}
});
});
HTML
<td width="2%" id="goToCostTypeID"> <a href="#" ><img src="../images/goto.png"/></a></td>
<div id="costContent">
<td width="13%" ><input type="hidden" id="cost_type_<%=i+1 %>" name="cost_type_<%=i+1 %>[]" value="<%=Bean.getLevelTwoOrgId()%>"/><%=Bean.getCcLevel2()%></td>
<td width="12%" ><input type="hidden" id="intival_<%=i+1 %>" name="intival_<%=i+1 %>[]" value="<%=Bean.getInitProj()%>"/><%=Bean.getInitProj()%></td>
JAVA
String costType = request.getParameter("costType");
uploadCostList =DAO.doGetAppCostUploadedList(PK_AppID,costType,iPresentYear);
当我点击图片时,我多次获得同一张桌子。请帮帮我。
问候, 萨兰亚C
【问题讨论】:
-
据我所知,您的条件逻辑完全被破坏了。它将始终返回“实际”。我不明白这个逻辑应该做什么。我建议您尝试删除这些条件并仅使用
document.getElementById("costType").value调用 ajax -
嗨 dganenco,我想根据 onclick 更改文本,例如实际、预算或预测,是的,你现在只有实际被多次调用。
-
你希望我如何更改 ajax 调用?你能解释一下吗?
-
首先页面加载了“实际”数据。当单击图像时,它应该加载预算等。所以我分配的值就像它在实际页面中并且用户点击文本中的下一个值应该更改为“预算”并加载预算数据
-
你可能想再看看你的逻辑。您正在进行 ajax 调用,这是对服务器的请求,当它完成时,在成功内,您使用 load 调用发出另一个请求。您应该能够使用 dganenco 的示例,或者使用您的第一个 ajax 请求的结果数据。