重定向页面并调用javascript函数 [英] Redirect page and call javascript function

查看:30
本文介绍了重定向页面并调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在提交时重定向到另一个页面 onClick 并调用一个进行 ajax 调用的函数.这就是我所拥有的:

I need to redirect to another page onClick of submit and call a function making an ajax call. This is what I have :

$('#submitButton').click(function() {

window.location.href = "otherPage";
displayData();
});

另外,我尝试的另一种方法是使用

Also, Another way I tried is to set the form fields on otherPage by using

var elem = window.document.getElementById(field);
elem.value = "new-value";

这样我就可以调用 otherPage 上的提交按钮的事件处理程序,但它不起作用.请让我知道我错在哪里.

so that I could call the eventhandler of a submit button present on otherPage, but it doesn't work. Please let me know where I am wrong.

推荐答案

我不确定是否有一种巧妙的方法来实现这一点,但是您可以在重定向到的 url 中添加一个哈希值,然后只需检查是否散列存在,执行函数并删除散列.

I'm not sure if there is a neat way to achieve this, but you can add a hash in your url you redirect to, then just simply check if the hash exists, execute function and remove hash.

以下是一些方便的网址:

Here are some handy URLs:

  • Location.Hash - information about this function and how to use it.
  • Removing hash from url without a page refresh - This was a bit of an issue, as window.location.href = ''; removes everything after the hash.

这篇关于重定向页面并调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆