简体   繁体   English

无法将变量移出要使用的函数

[英]Can't move variable outside of function to be used

I am really struggling with this. 我真的为此感到挣扎。 I have created a form which calculates what the person owes based on how many people in a tent and if they are paying by credit card. 我创建了一个表格,该表格根据帐篷中有多少人以及他们是否使用信用卡付款来计算该人的欠款。 The amounts update depending on the selection. 金额根据选择而更新。 When the user submits they should be redirected to PayPal with the total amount on the end of the url, Ie: 当用户提交时,应将其重定向到PayPal,并将总金额显示在url的末尾,即:

PayPal.me/240 for £240 or PayPal.me/600 for £600 PayPal.me/240(240英镑)或PayPal.me/600(600英镑)

The issue is my price variable is outside of my other function which sets the redirect, and the price amount never makes it to the redirect. 问题是我的价格变量不在我设置重定向的其他函数的范围内,并且价格金额从未进入重定向。

This is what I have tried: 这是我尝试过的:

HTML: HTML:

    <select name='field_0' id='field_0' class='text_select' onchange="pricecalc()"   >  
<option value="2 people" >2 people</option> 
<option value="3 people" >3 people</option> 
<option value="4 people" >4 people</option> 
<option value="5 people" >5 people</option> 
<option value="6 people" >6 people</option> 
</select>

    <select name='field_1' id='field_1' class='text_select' onchange="pricecalc()"   >  
<option value="Bank transfer" >Bank transfer</option>   
<option value="Debit card" >Debit card</option> 
<option value="Credit card + 3.4%" >Credit card + 3.4%</option> 
</select>         
<b>Total Price</b>
<span id="cost">Hello World!</span>
<b>Price Each</b>
<span id="costeach">Hello World!</span>

Relevant JS: 相关JS:

var myprice;
function pricecalc() {
  var a = document.getElementById("field_0");
  var quantity = a.value.substring(0, 1);
  var b = document.getElementById("field_1");
  var type = b.value;

  if (quantity == '2') {
    var rate = '120';
  } else if (quantity == '3') {
    var rate = '110';
  } else {
    var rate = '100';
  }

  myprice = rate * quantity;
  if (type == 'Credit card + 3.4%') {
    myprice = myprice * 1.034;
  }

  var price_each = (myprice / quantity);
  document.getElementById("cost").innerHTML = myprice;
  document.getElementById("costeach").innerHTML = price_each;
}

function PHPFMG( formID ){
    var redirect = 'https://www.paypal.me/tim/' + myprice;

I would recommend doing this instead 我建议这样做

function pricecalc() {
  var myprice;
  var a = document.getElementById("field_0");
  var quantity = a.value.substring(0, 1);
  var b = document.getElementById("field_1");
  var type = b.value;

  if (quantity == '2') {
    var rate = '120';
  } else if (quantity == '3') {
    var rate = '110';
  } else {
    var rate = '100';
  }

  myprice = rate * quantity;
  if (type == 'Credit card + 3.4%') {
    myprice = myprice * 1.034;
  }

  var price_each = (myprice / quantity);
  document.getElementById("cost").innerHTML = myprice;
  document.getElementById("costeach").innerHTML = price_each;

  return myprice;
}

Then 然后

function PHPFMG( formID ){
    var myprice = pricecalc();
    var redirect = 'https://www.paypal.me/tim/' + myprice;
}

This way you don't polute the global scope 这样您就不会污染全局范围

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

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