我正在尝试使用JSON删除一行数据,但是当我提示确认对话框时,我的javascript函数无法正常工作:
<script type="text/javascript">
$().ready(function() {
$("a.delete").click(function() {
$.ajax({
type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
success: function(msg) {
if (msg.status == "ok") {
$("tr#" + msg.id).hide();
}
else {
alert(msg.exception);
}
}
});
return false;
});
});
</script>
以上工作绝对没问题,但是我把以下内容写进了:
<script type="text/javascript">
$().ready(function() {
$("a.delete").click(function() {
if (!confirm("Are you sure you want to delete this?")) return false;
$.ajax({
type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
success: function(msg) {
if (msg.status == "ok") {
$("tr#" + msg.id).hide();
}
else {
alert(msg.exception);
}
}
});
return false;
});
});
</script>
这个执行执行删除,但它不会隐藏表行,这让我觉得它还没有被删除。有什么想法吗?
答案 0 :(得分:1)
试试这个:
<script type="text/javascript">
$().ready(function() {
$("a.delete").click(function() {
if (confirm("Are you sure you want to delete this?")){
$.ajax({
type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
success: function(msg) {
if (msg.status == "ok") {
$("tr#" + msg.id).hide();
}
else {
alert(msg.exception);
}
}
});
}
return false;
});
});
</script>
希望它适合你...