【发布时间】:2021-06-21 01:24:14
【问题描述】:
我正在将数据插入数据库并在页面上显示数据而不刷新页面。
我尝试了$("#e-empListwrap").load(location.href + "#e-empListwrap");,但它在我的页面上显示了整个页面。
$("form[name='emp']").validate({
rules: {
//rules
},
submitHandler: function(form) {
var form = $('form[name="emp"]').get(0);
var formData = new FormData(form);
$.ajax({
url: baseUrl + "/controller/emp_control.php",
type: "POST",
dataType: 'json',
data: formData,
contentType: false,
cache: false,
processData: false,
success: function(data) {
$('form[name="emp"]')[0].reset();
$('#modal-addemp').modal().hide();
$("#e-empListwrap").load(location.href + "#e-empListwrap");
},
}); // AJAX Get Jquery statment
}
});
HTML
<div class="e-empListwrap mt-4" id="e-empListwrap"><ul></ul></div>
// The below is the script when refersh the page then it will call the ajax and get the the and display on the page.
<script>
fetchEMPsaved_data();
function fetchEMPsaved_data(){
$.ajax({
url:'controller/developer_control.php',
type: "post",
dataType: "json",
data: { action: "fetchEMPsaved_data"},
success: function(data) {
$("#e-empListwrap ul").append(data);
}
});
}
</script>
【问题讨论】:
-
这条语句
$("#e-empListwrap").load(location.href + "#e-empListwrap");要求 JS 将您当前的网页加载到一个元素中。你从你的ajax函数返回data,为什么不直接使用呢?
标签: javascript html jquery ajax