简体   繁体   English

在window.location重定向后使用jquery ajax在下一页的div中写一个html文本

[英]Write an html text inside a div of the next page after window.location redirect using jquery ajax

Is there a way to call a (jquery action/write an html text) to a div of a new page after calling the window.location command? 有没有办法在调用window.location命令后调用(jquery动作/写一个html文本)到新页面的div?

Im trying to make an ajax form submit where the user will be redirected to a new page upon submit, 我试图制作一个ajax表单提交,用户将在提交时重定向到新页面,

and in that new page a hidden div will appear with text inside of it 在该新页面中,隐藏的div将显示在其中的文本

currently this is my code 目前这是我的代码

script.js 的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;
                }
            }

        });

i want to make this block of code below work on the page where window.location is going 我想让下面的代码块在window.location所在的页面上工作

                $("#status").attr('class', 'span12 alert alert-error');
                $("#status").show();
                $("#status").html("<h4><center>Successfully added Department</center></h4>");

is it possible? 可能吗?

thanks 谢谢

You can use CI session flash data. 您可以使用CI会话闪存数据。

http://ellislab.com/codeigniter/user-guide/libraries/sessions.html http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Before triggering window.location , set the message in flashdata . 在触发window.location之前,请在flashdata设置消息。 On the landing/redirected-to page, check to see if flashdata has a value (success/failure). 在登陆/重定向到页面上,检查flashdata是否具有值(成功/失败)。 If so, display (or trigger a js method to show) the message. 如果是,则显示(或触发js方法以显示)消息。

You could add a parameter with your window.location, like: 您可以在window.location中添加一个参数,例如:

window.location = base + 'admin/departments/edit_dept/' + result + '?showMessage=12'

Then on the next page, have a jquery script that looks for that parameter and shows the message. 然后在下一页上,有一个jquery脚本,用于查找该参数并显示该消息。 See this question . 看到这个问题

Or you can do it in on the server. 或者你可以在服务器上进行。 But with jquery it works with static html too. 但是使用jquery它也适用于静态html。

This is a patchup, May require some tuning though. 这是一个补丁,可能需要一些调整。

window.location = base + 'admin/departments/edit_dept/' + result+'/err';  //changed to catchup with the view part

In the Controller: 在控制器中:

 <?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);
    }
?>

In the view part: 在视图部分:

<?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM