[英]How to redirect to another html page without using window.location?
[英]Write an html text inside a div of the next page after window.location redirect using jquery ajax
有沒有辦法在調用window.location命令后調用(jquery動作/寫一個html文本)到新頁面的div?
我試圖制作一個ajax表單提交,用戶將在提交時重定向到新頁面,
在該新頁面中,隱藏的div將顯示在其中的文本
目前這是我的代碼
的script.js
$.ajax({
url:url,
type:'POST',
data:datastr,
success:function(result){
if(result=="duplicate"){
$("#status").attr('class', 'span12 alert alert-error');
$("#status").show();
$("#status").html("<h4><center>Duplicate Username</center></h4>");
$("#dept_name").closest(".control-group").attr('class', 'control-group');
$("#username").closest(".control-group").attr('class', 'control-group error');
$("#password").closest(".control-group").attr('class', 'control-group');
$("#username").focus();
}
else{
$("#dept_name").closest(".control-group").attr('class', 'control-group');
$("#username").closest(".control-group").attr('class', 'control-group');
$("#password").closest(".control-group").attr('class', 'control-group');
window.location = base + 'admin/departments/edit_dept/' + result;
}
}
});
我想讓下面的代碼塊在window.location所在的頁面上工作
$("#status").attr('class', 'span12 alert alert-error');
$("#status").show();
$("#status").html("<h4><center>Successfully added Department</center></h4>");
可能嗎?
謝謝
您可以使用CI會話閃存數據。
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
在觸發window.location
之前,請在flashdata
設置消息。 在登陸/重定向到頁面上,檢查flashdata
是否具有值(成功/失敗)。 如果是,則顯示(或觸發js方法以顯示)消息。
您可以在window.location中添加一個參數,例如:
window.location = base + 'admin/departments/edit_dept/' + result + '?showMessage=12'
然后在下一頁上,有一個jquery腳本,用於查找該參數並顯示該消息。 看到這個問題 。
或者你可以在服務器上進行。 但是使用jquery它也適用於靜態html。
這是一個補丁,可能需要一些調整。
window.location = base + 'admin/departments/edit_dept/' + result+'/err'; //changed to catchup with the view part
在控制器中:
<?php
if($this->uri->segment(4) == "err"){
$data['err'] = true; #will reflect that we need to show the js in the view
$this->load->view('view', $data);
}
?>
在視圖部分:
<?php if(isset($err)){ ?>
<script type="text/javascript">
$("#status").attr('class', 'span12 alert alert-error');
$("#status").show();
$("#status").html("<h4><center>Successfully added Department</center></h4>");
</script>
<?php } ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.