jquery ajax,如何在处理页面中调用onload事件?

发布于 2024-11-08 05:44:17 字数 1219 浏览 4 评论 0原文

我尝试通过jquery ajax将值从页面a发布到页面b。 但是当我需要在句柄页面中有一个onload事件时。我尝试了2种方法,但都失败了。如何正确调用还是有bug?

a.php

<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">     
jQuery(document).ready(function(){
    $("a").click(function(){
     $.ajax({
         url: "b.php", 
         dataType: "html",
         type: 'POST', 
         data: "word="+"hello", 
         success: function(data){ 
            $("#show").html(data);
         }
      });
    });
});
</script>
<a href="#">click</a>
<div id="show"></div>

b.php

<script language="JavaScript">
   function callhello() {
       alert('<?php echo $_POST['word']; ?>');
       //many other code, for a test, I moved them. and replace a alert call.
   }
</script>
<body onload="JavaScript:callhello()"><!-- way 1, put onload here, not run -->
    Blah~~ Blah~~ Blah~~
<script language="JavaScript">// change to text/javascript or even remove, no effect
window.onload = function() {
  callhello();
};
</script><!-- way 2, put onload here, still not run. -->
</body>

I have try to post value from page a to page b via jquery ajax.
But when I need a onload event in the handle page. I tried 2 ways, but all failed. How to call correctly or it is a bug?

a.php

<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">     
jQuery(document).ready(function(){
    $("a").click(function(){
     $.ajax({
         url: "b.php", 
         dataType: "html",
         type: 'POST', 
         data: "word="+"hello", 
         success: function(data){ 
            $("#show").html(data);
         }
      });
    });
});
</script>
<a href="#">click</a>
<div id="show"></div>

b.php

<script language="JavaScript">
   function callhello() {
       alert('<?php echo $_POST['word']; ?>');
       //many other code, for a test, I moved them. and replace a alert call.
   }
</script>
<body onload="JavaScript:callhello()"><!-- way 1, put onload here, not run -->
    Blah~~ Blah~~ Blah~~
<script language="JavaScript">// change to text/javascript or even remove, no effect
window.onload = function() {
  callhello();
};
</script><!-- way 2, put onload here, still not run. -->
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷离° 2024-11-15 05:44:17

当您将 HTML 插入文档时,所有脚本都会在文档的上下文中执行。这意味着window.onload引用当前窗口load事件。由于插入被延迟(通过等待 click 事件),因此 window.onload 事件已经被触发。您可以将一个函数附加到 load 事件,但它永远不会被触发。

最好将函数调用直接放入 script 元素中:

<script type="text/javascript"> // the language attribute is deprecated
  callhello();
</script>

这意味着 jQuery 将在脚本添加到文档时执行该脚本。

When you insert HTML into your document, any scripts are executed with the document's context. That means that window.onload refers to the load event of the current window. Given that the insertion is delayed (by waiting for the click event), the window.onload event has already been triggered. You can attach a function to the load event, but it will never be triggered.

It's probably best to put the function call straight into your script element:

<script type="text/javascript"> // the language attribute is deprecated
  callhello();
</script>

This means that jQuery will execute the script as it's added to the document.

暮年 2024-11-15 05:44:17

您需要更改

<script language="JavaScript">

为:

<script type="text/javascript">

您需要更改

function callhello() {
    alert('<?php echo $_POST['word']; ?>');
    //many other code, for a test, I moved them. and replace a alert call.
}

为:

function callhello() {
    alert('<?php echo $_POST["word"]; ?>');
    //many other code, for a test, I moved them. and replace a alert call.
}

您需要更改

<body onload="JavaScript:callhello()">

为:

<body onload="javascript://callhello();">

You need to change:

<script language="JavaScript">

to:

<script type="text/javascript">

You need to change:

function callhello() {
    alert('<?php echo $_POST['word']; ?>');
    //many other code, for a test, I moved them. and replace a alert call.
}

to:

function callhello() {
    alert('<?php echo $_POST["word"]; ?>');
    //many other code, for a test, I moved them. and replace a alert call.
}

You need to change:

<body onload="JavaScript:callhello()">

to:

<body onload="javascript://callhello();">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文