我有一个 AJAX 函数,它使用简单的 select *
... 从数据库加载数据到 div。该功能在没有提交的情况下工作正常,但是当我使用表单时,它不起作用并且 #itemContainer
是空的,我是不是错过了什么?我什至尝试过:
$(document).ready(function() {
$("#myForm").submit(function() {
但也没有用
我的代码:
<script id="source" language="javascript" type="text/javascript">
$("#myForm").submit(function() {
$.ajax({
url: 'models/fetchUsers.php', //the script to call to get data
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
$.each($(data), function(key, value) {
$('#itemContainer').append(value.user_id);
});
}
});
});
</script>
最佳答案
您没有取消表单提交事件。
在你的提交中添加preventDefault()
像这样
$("#myForm").submit(function(event) {
$.ajax({
url: 'models/fetchUsers.php', //the script to call to get data
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
$.each($(data), function(key, value) {
$('#itemContainer').append(value.user_id);
});
}
});
event.preventDefault();
});
更新:
event.preventDefault()
已删除。
尝试使用return false;
像这样
$("#myForm").submit(function(event) {
$.ajax({
url: 'models/fetchUsers.php', //the script to call to get data
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
$.each($(data), function(key, value) {
$('#itemContainer').append(value.user_id);
});
}
});
return false;
});
关于javascript - jQuery .append 不适用于 .submit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39569658/