我有跟随代码,假设在div中显示加载的数据,id = bodyContainer但是重定向到url ...
function viewCategory() {
$.get('/management/Category', function(data){ //received data
$('#bodyContainer').html(data); //doesnt show in bodycontainer instead redirects to get method url
}); }
</script>
<body>
<table class="table">
<tr>
<td><a href="${pageContext.request.contextPath}/assetCategory" onclick="viewCategory()"> View Category</a>
</td>
</tr>
</table>
<div class="col-sm-10 col-md-10 col-lg-10" id="bodyContainer" >Welcome </div>
答案 0 :(得分:0)
问题是您为href
标记指定了a
,它显然会重定向。如果您希望ajax
在这里,您应该阻止a
的默认操作,或者只删除href
值并替换为#
或{{1} }。
案例1
javascript:void(0)
为此
绑定<a href="${pageContext.request.contextPath}/assetCategory" class="getData"> View Category</a>
Event click
案例2
$('a.getData').on('click',function(e){
e.preventDefault(); //will prevent from redirecting
//Also you can use `$.load` instead of `get`
$('#bodyContainer').load('/management/Category', function(data){
//Anything to execute after load is complete
});
});
您可以使用<a href="javascript:void(0);" onclick="viewCategory()">
或删除function
并将其与onlick
绑定,如案例1
jquery