简体   繁体   English

在另一页上使用getElementById

[英]Using getElementById on another page

I'm trying to create a "Same As Shipping" Button that's seen on many online ordering platforms that, when clicked/checked during the billing step of the transaction, imports the information from another page. 我正在尝试创建一个在许多在线订购平台上都可以看到的“与运送相同”按钮,当在交易的结算步骤中单击/选中该按钮时,该信息将从另一个页面导入。 I looked at the other question on this topic and didn't find anything helpful. 我查看了关于该主题的另一个问题 ,但没有发现任何帮助。 I do know that the two pages fall under the Same Origin Policy. 我确实知道这两个页面属于同一原产地政策。

I've got the Javascript code created to populate the billing page with information. 我已经创建了Javascript代码,以使用信息填充结算页面。 However, I cannot find a way to bring the value from the other page. 但是,我找不到从其他页面获取价值的方法。

<script language="JavaScript">
function SameAs(){
    document.getElementById("FIELD_4").value = "752";
    document.getElementById("FIELD_6").value = "50";
}
</script>

My problem is that I can't bring down the element from the previous page to use as the value. 我的问题是我无法从上一页中删除该元素以用作值。

Save it in local storage on the first page 将其保存在第一页的本地存储中

localStorage.setItem('city', $('#elementID').val());

Then retrieve it from the second 然后从第二个检索

localStorage.getItem('city');

You're trying to fetch an element from a page that you are no longer on? 您是否正在尝试从不再访问的页面中获取元素? This is impossible. 这是不可能的。 You can only get elements that currently exist in the DOM. 您只能获取DOM中当前存在的元素。

You need to store the information either in an encrypted cookie (for privacy reasons, you should never save payment information though) or request it via AJAX from the second page. 您需要将信息存储在加密的cookie中(出于隐私原因,尽管您永远不要保存付款信息),或者从第二页通过AJAX请求信息。 There is no way to get the information from a previously submitted page. 无法从先前提交的页面获取信息。

Another option is not to actually submit a form from the prior page, but instead show the next page by making it visible via Javascript. 另一个选择是不从上一页实际提交表单,而是通过在Javascript中使其可见来显示下一页。 When both "pages" are ready, then you can submit the form. 当两个“页面”都准备就绪时,您可以提交表单。

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

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