我有一个表格,其中列出了用户输入的值,旁边有 2 个按钮用于删除或标记为已完成。在页面上,表格是可见的,有 3 个选项卡,我们将它们称为 Tab1、Tab2 和 Tab3
每个选项卡都有一个表(如上所述),其中包含有关特定类型条目的信息。
这些按钮很简单<a href>
链接,所以当点击它们时,它们会重新加载页面。这是一个问题,因为用户 View 会刷新,并且会将选项卡恢复为默认选项卡,并且在尝试标记多个条目时也会带来不便。
我想让这些按钮向另一个页面发送 Ajax 请求以处理数据。唯一的问题是,我不太确定如何进行 ajax 调用。
这是我现在拥有的
我的按钮
echo "<td class='td-actions'>";
echo " <a href='?complete=".$row['uniqueID']."' class='btn btn-success btn-small'>
<i class='btn-fa fa-only fa fa-check'> </i>
</a>
<a href='?remove=".$row['uniqueID']."' class='btn btn-danger btn-small'>
<i class='btn-fa fa-only fa fa-remove'> </i>
</a>";
echo "</td>";
有一种叫做 Complete,一种叫做 Remove。
当按下其中任何一个时,它当前会重新加载页面,这会触发一些 php if 语句。
if(isSet($_GET['remove'])) {
$sql = "DELETE from rl_logged where uniqueID='".$_GET['remove']."';";
$ret = $db->exec($sql);
echo "<meta http-equiv='refresh' content='0;index.php' />";
}
if(isSet($_GET['complete'])) {
$sql = "UPDATE rl_logged set complete=1 where uniqueID='".$_GET['complete']."';";
$ret = $db->exec($sql);
echo "<meta http-equiv='refresh' content='0;index.php' />";
}
这些都是比较简单的函数。我的问题是我不太了解 javascript。
如有任何帮助,我们将不胜感激。
我想出的javascript是这个
$(document).ready(function() {
$('#markComplete').click(function() {
var input = input = $(this).text()
$.ajax({ // create an AJAX call...
data: {
onionID: input,
},
type: 'POST', // GET or POST from the form
url: 'pages/ajax/markCompleteRL.php', // the file to call from the form
success: function(response) { // on success..
refreshAllTabsWithFade();
}
});
});
});
使用这个按钮
<div name='markComplete' id='markComplete' class='btn btn-success btn-small'>
<i class='btn-fa fa-only fa fa-check'></i>".$row['uniqueID']."
</div>
但是,在使用 firebug 检查时,这似乎曾经有效,但现在按钮没有做任何事情。
今天早上我又试了一次,按下按钮,第一次发送这篇文章,然后按钮不再执行 - 即使在页面重新加载时也是如此。
最佳答案
我能够让它与以下项目一起工作:
JavaScript:
$('.markComplete').submit( function( event ) {
event.preventDefault();
event.stopImmediatePropagation();
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // serialize the form
type: "POST", // GET or POST from the form
url: "pages/ajax/repairlogMarks.php", // the file to call from the form
success: function(response) { // on success..
refreshAllTabs();
}
});
return false;
});
按钮:
<form class="markComplete">
<input type="text" style="display:none;" class="form-control" name="uniqueid" value='<?=$row['uniqueID'];?>'>
<input type="text" style="display:none;" class="form-control" name="markcomp" value='1'>
<button type="submit" class="btn btn-success">
<i class="btn-fa fa-only fa fa-check"></i>
</button>
</form>
基本上,我将按钮制作成一个我知道如何为其创建 ajax 请求的表单。
-- 更新以使其适用于多个按钮,这些按钮对不同的唯一 ID 执行相同的功能。
关于javascript - aJax 使用按钮更新 sqlite 和 php 中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39043268/