Jquery 使用按钮单击ajax获取新数据
我需要有关如何在单击按钮时刷新数据的帮助。它所做的是每次我点击按钮都复制。请帮忙 这是我的jquery代码Jquery 使用按钮单击ajax获取新数据,jquery,ajax,Jquery,Ajax,我需要有关如何在单击按钮时刷新数据的帮助。它所做的是每次我点击按钮都复制。请帮忙 这是我的jquery代码 $(document).ready(function() { $('#button1').click(function(event) { $.ajax({ url : 'http://localhost/api/eric_api.php?q=series', type : 'GET', dataType : 'json',
$(document).ready(function() {
$('#button1').click(function(event) {
$.ajax({
url : 'http://localhost/api/eric_api.php?q=series',
type : 'GET',
dataType : 'json',
success : function(data) {
var table = $("#list");
$.each(data, function(idx, elem) {
table.append("<tr><td>" + elem.user + "</td></tr>");
});
},
error : function() {
alert('There was an error');
}
});
});
});
$(文档).ready(函数(){
$('#按钮1')。单击(函数(事件){
$.ajax({
网址:'http://localhost/api/eric_api.php?q=series',
键入:“GET”,
数据类型:“json”,
成功:功能(数据){
var表=$(“#列表”);
$.each(数据、函数(idx、元素){
table.append(“+elem.user+”);
});
},
错误:函数(){
警报(“出现错误”);
}
});
});
});
我的html代码是
<?php include 'eric_api.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="js/jquery.js"></script>
<script src="js/api_calls.js"></script>
<link rel="stylesheet" href="css/normalizer.css" />
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div>
<table id="list"></table>
</div>
<div>
<button id="button1">Get Data</button>
</div>
</body>
获取数据
我得到了正确的返回数据,我只想刷新数据和重复的值级联到我的页面。谢谢。这只是一个猜测,但当然你必须先移除旧的入口,然后再像:
table.empty();
$.each(data, function(idx, elem) {
table.append("<tr><td>" + elem.user + "</td></tr>");
});
table.empty();
$.each(数据、函数(idx、元素){
table.append(“+elem.user+”);
});
试试这个..你应该清除旧数据以附加新数据
$('#button1').click(function(event) {
$("#list").empty();//Clear old data before ajax
$.ajax({
url : 'http://localhost/api/eric_api.php?q=series',
type : 'GET',
dataType : 'json',
success : function(data) {
var table = $("#list");
$.each(data, function(idx, elem) {
table.append("<tr><td>" + elem.user + "</td></tr>");
});
},
error : function() {
alert('There was an error');
}
});
});
$('#按钮1')。单击(函数(事件){
$(“#list”).empty();//在ajax之前清除旧数据
$.ajax({
网址:'http://localhost/api/eric_api.php?q=series',
键入:“GET”,
数据类型:“json”,
成功:功能(数据){
var表=$(“#列表”);
$.each(数据、函数(idx、元素){
table.append(“+elem.user+”);
});
},
错误:函数(){
警报(“出现错误”);
}
});
});
试试这个
success : function(data) {
var table = $("#list");
table.html(''); // empty table on success
$.each(data, function(idx, elem) {
table.append("<tr><td>" + elem.user + "</td></tr>");
});
}
成功:函数(数据){
var表=$(“#列表”);
html(“”);//成功时为空表
$.each(数据、函数(idx、元素){
table.append(“+elem.user+”);
});
}
感谢您的快速回复,我尝试过了,但并没有从数据库返回所有值。Sridhar和demouser都是正确的。谢谢,我不能把支票都放在上面,所以我会用第一个答复。谢谢你们。很抱歉,在循环中清空表是错误的,我更新了答案谢谢你们的回复,它仍然没有返回它应该返回的所有值。当它应该是两行时,它只返回一行。每当您单击按钮时,它将调用并清除旧数据,并从数据库中获取新数据