使用 Javascript 变量打开 Iframe

我尝试将 javascript 变量传递到 iframe 的 url 中,但不知何故我做错了;谁能告诉我我犯了哪个错误?


代码:


function getPrice() {

    var plans = document.getElementsByName('plans');

    for (index=0; index < plans.length; index++) {

        if (plans[index].checked) {

            return plans[index].getAttribute('data-price');

            break;

        }

    }

这返回;50、100 或 150 作为值。


<button OnClick="javascript:alert(getPrice() + '.00');">Bedrag</button>

此按钮为我提供了想要添加到 Iframe 的 url 中的所需值:


<iframe width="350" height="150" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:0px;" src="https://www.website.com/create-ideal-payment.php?betaling="+ getPrice() + ".00"></iframe>

不幸的是,I 帧打开时没有得到函数所需的输出"getPrice()"。


我的哪一部分想法不正确,无法将正确的金额传递给 Iframe 中的 url?


提前感谢所有支持/帮助!


泛舟湖上清波郎朗
浏览 139回答 2
2回答

GCT1015

在 javascript 本身中设置 iFrame URL。function getPrice() {&nbsp; &nbsp; var plans = document.getElementsByName('plans');&nbsp; &nbsp; var paymentFrame = document.getElementById('paymentFrame');&nbsp; &nbsp; var selectedPlan;&nbsp; &nbsp; for (index=0; index < plans.length; index++) {&nbsp; &nbsp; &nbsp; &nbsp; if (plans[index].checked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedPlan = plans[index].getAttribute('data-price');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; paymentFrame.src="https://www.website.com/create-ideal-payment.php?betaling="+ selectedPlan + ".00"&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return selectedPlan;}<input type="checkbox" name="plans" data-price="50"><input type="checkbox" name="plans" data-price="100"><input type="checkbox" name="plans" data-price="150"><button OnClick="javascript:alert(getPrice() + '.00');">Bedrag</button><iframe id="paymentFrame" width="350" height="150" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:0px;"></iframe>

元芳怎么了

在 HTML 主要中记下 iframe 的 id&nbsp;<iframe width="350" id = "IframeID" height="150" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:0px;" ></iframe>主要 HTML 中引用的 JavaScript&nbsp; function getPrice() {&nbsp; &nbsp; &nbsp; var plans = document.getElementsByName('plans');&nbsp; &nbsp; &nbsp; for (index=0; index < plans.length; index++) {&nbsp; &nbsp; &nbsp; if (plans[index].checked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.getElementById('result').innerText = 50;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return 50;//plans[index].getAttribute('data-price');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp;}&nbsp;}&nbsp; // For testinhg this is the value will be injected in URL of iframe as query string&nbsp;return 50}window.addEventListener('DOMContentLoaded', (event) => {&nbsp; &nbsp; console.log(getPrice());&nbsp; &nbsp; document.getElementById('IframeID').src = "somepage.html?seed=" + getPrice() + ".00";});IFrame 源 HTML<html>&nbsp; &nbsp;<head>&nbsp; &nbsp;<meta charset="UTF-8">&nbsp; &nbsp;<meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp;<title>Document</title></head><body>&nbsp; &nbsp; <h1>TEST in IFRame</h1></body><script>&nbsp; &nbsp;function getParams() {&nbsp; &nbsp; &nbsp; var params = {},&nbsp; &nbsp; &nbsp; pairs = document.URL.split('?')&nbsp; &nbsp; &nbsp; &nbsp; .pop()&nbsp; &nbsp; &nbsp; &nbsp; .split('&');&nbsp; &nbsp;for (var i = 0, p; i < pairs.length; i++) {&nbsp; &nbsp; p = pairs[i].split('=');&nbsp; &nbsp; params[ p[0] ] =&nbsp; p[1];&nbsp; }&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return params;&nbsp; }&nbsp;var parameters = getParams();&nbsp;for( var i in parameters ){&nbsp; &nbsp;console.log( i , parameters[i] );}&nbsp;</script>&nbsp;</html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript