简体   繁体   English

简单的jQuery事件处理程序

[英]simple jquery event handler

having some real problems with jquery at the moment. 目前在jquery上存在一些实际问题。 Basically what I have so far is. 到目前为止,基本上我是。 The form is submitted once the form is submitted a grey box pop's up with the relevant infomation. 表单提交后,表单就会被提交,并弹出带有相关信息的灰色框。

What I need to do though is refresh the whole page then allow the grey box to appear. 我需要做的是刷新整个页面,然后显示灰色框。

I have the following code 我有以下代码

 $("#ex1Act").submit(function() {
        //$('#example1').load('index.php', function()                                 
        $("#example1").gbxShow();
        return true;
      });

the line which is commented out load's the page again after the form is submitted the other code makes the grey box pop-up. 提交表单后,被注释掉的行再次加载页面,其他代码使灰色框弹出。

Is their a way to say once the: 是他们曾经说过的一种方式:

$('#example1').load('index.php', function() 

has been exucted do this: 已经被引诱这样做:

 $("#example1").gbxShow();

hope this makes sense. 希望这是有道理的。

This is not possible. 这是不可能的。

Once the form is submitted, the Javascript running on the page that submitted the form is completely gone; 提交表单后,在提交表单的页面上运行的JavaScript完全消失了; it cannot affect the page that the form returns. 它不会影响表单返回的页面。

Instead, you should put server-side code in the form that writes $("#example1").gbxShow(); 相反,您应该以写$("#example1").gbxShow();的形式放置服务器端代码$("#example1").gbxShow(); in a separate <script> block if the form has been submitted. 如果已提交表单,则在单独的<script>块中。

Why not just submit the form normally (ie, not using JavaScript) and add a variable to the resulting page signalling the need to display the grey box? 为什么不正常地提交表单(即不使用JavaScript)并在结果页面中添加一个变量,以表示需要显示灰色框呢? Like so: 像这样:

<?php if(isset($_POST['submit'])): ?>
    <script type="text/javascript">
        var showGreyBox = true;
    </script>
<?php endif; ?>

... ...

<script type="text/javascript">
    $(function(){
        if(showGreyBox !== undefined){
            // execute code to show the grey box
        }
    });
</script>

Something like that, maybe? 这样的事吧?

The problem you have is that the web page is "stateless". 您遇到的问题是该网页是“无状态的”。 This means that you can't do a bit of JavaScript, refresh the page and continue on with your JavaScript. 这意味着您将无法使用JavaScript,刷新页面并继续使用JavaScript。 When you refresh the page, you lose your current state and the page starts from scratch. 刷新页面时,您将丢失当前状态,并且页面将从头开始。

You will need to re-engineer your design to bear in mind the page lifecycle (ie all JavaScript stops permanently on navigation). 您需要重新设计您的设计,以记住页面生命周期(即所有JavaScript在导航上永久停止)。

One solution may be to use the jQuery AJAX forms plugin, which will submit the form to the server and give you back the result of the submission, which would avoid breaking the page lifecycle. 一种解决方案可能是使用jQuery AJAX表单插件,该插件会将表单提交到服务器并返回提交结果,这将避免破坏页面生命周期。 You could then display the box as you wish. 然后,您可以根据需要显示该框。

The standard way to do this is to have the server return the gray box contents in the response to the form post. 执行此操作的标准方法是让服务器在对表单发布的响应中返回灰框内容。

You could probably do this in jquery by putting the page and the grey box into separate iFrames so that it would be safe from the page refresh 您可以在jquery中通过将页面和灰色框放入单独的iFrame中来做到这一点,以便从页面刷新中安全

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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