当我点击图书链接时,他会重新加载页面并对列进行排序。 我想用ajax停止重装,,,怎么样?
这是我对这个过程的作用。
这个getBook函数
public function getBooks($start = 0, $limit = 2, $order = "ASC")
{
$sql_start = $start * $limit;
$sql_limit = $limit;
$sql_order_by = $order;
$query = "SELECT Library.nameOfBook, userBook.book_id, userBook.user_id FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username ORDER BY Library.nameOfBook $sql_order_by LIMIT $sql_start, $sql_limit";
$statment = $this->db->prepare($query);
$statment->execute([
':username' => $this->username
]);
$result = $statment->fetchAll();
echo "<table id='myTable' border='1'>
<tr>
<th><a id='sorter' href='#'>Books</a></th>
<th>Action</th>
</tr>";
foreach($result as $row){
echo "<tr>";
echo "<td>" . $row['nameOfBook'] . "</td>";
echo "<td>" ."<input type='submit' id='delete".$row['book_id']."-".$row['user_id']."' onclick='deleteBook(this)' name='delete' value='Delete'>" . "</td>";
echo "</tr>";
}
echo "</table>";
echo "";
return count($result);
}
这个jquery函数
<script>
$(document).ready(function() {
$( "#sorter" ).click(function() {
var order_by_value = $('input[name="order_by"]').val();
if(order_by_value == "ASC"){
$('input[name="order_by"]').val("DESC");
}
else {
$('input[name="order_by"]').val("ASC");
}
$('input[name="current"]').trigger('click');
});
});
</script>
答案 0 :(得分:0)
<script>
$(document).ready(function() {
$( "#sorter" ).click(function() {
var order_by_value = $('input[name="order_by"]').val();
$.ajax({
type: 'GET',
url: 'the url link ',
data: {
'order_by_value': order_by_value
},
success: function (res) {
if(res == "ASC"){
$('input[name="order_by"]').val("ASC");
}
else {
$('input[name="order_by"]').val("DSC");
}
$('input[name="current"]').trigger('click');
});
}
});
</script>
答案 1 :(得分:-1)
首先,在确定ajax已完成其过程之前,请不要重新加载页面。
其次,如果您希望在完成提交表单中的表单或数据后使用JS运行该代码。
以下是您在竞争中重新加载页面的ajax代码如何显示
$.ajax({
type: 'GET',
url: 'your_page_link.php',//this will be url of your backend page
data: {
'name':name
'email':email
},// if you are using form then just use .serialize() to submit the form instead of preparing data like this use $("#form_id").serialize()
success: function (msg) {
resp = msg;
}
}).done(function(){
if(resp == your_expected_response){
window.location.href = "http://stackoverflow.com";
}else{
//your error handler
}
});
希望这会有所帮助,请随时询问是否需要澄清..
干杯!!