如果我点击书籍链接,他会按升序对书进行排序,如果我再次点击,他会按降序排序,但他没有做这个动作,除非我点击下一个或上一次提交,为什么他没有&# 39; t更新页面没有点击下一个或上一个?
$(document).ready(function() {
$("#sorter").click(function(e) {
e.preventDefault();
var order_by_value = $('input[name="order_by"]').val();
$.ajax({
type: 'GET',
url: "" + 'http://test.local/UUser.php?action=getBooks',
data:{
'order_by_value': order_by_value,
// button: 'button',
},
success: function (res) {
console.log(res);
if(order_by_value == "ASC"){
$('input[name="order_by"]').val("DESC");
}
else {
$('input[name="order_by"]').val("ASC");
}
// $('input[name="current"]').trigger('click');
}
});
});
});
这是我的功能
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);
}
答案 0 :(得分:1)
很难从AJAX函数本身分辨出来。逻辑似乎很好。也许您没有针对特定元素,或者HTML代码的结构对于您尝试实现的目标存在一些逻辑缺陷?