开发者

javascript: how do i push a radio value to another function?

can't get the variable taxRate to be seen in the function called by the following form. blargh! :P

here's the HTML:

<form name="incomeRange" id="incomeRange" title="Taxable Income Ranges">
  <legend>Income Range:</legend>            
    <p name="incomeTable" id="incomeTable">
      please make a selexion: <br>
      <label>
        <input type="radio" name="B" id="B_1" value="200" onChange="var taxRate = (this.value);">200
  </label>
</p>
</form>
<form name="userAmount" id="userAmount">
  <p>Target Amount to Spend:
    <input type="text" name="userTargetAmount" id="userTargetAmount" onKeyUp="doTheMath(this.value);">
  </p>
</form>

and the function it calls:

function doTheMath(amount)
{
  var targAmt = document.getElementById("targetAmount");
  var taxAmt = document.getElementById("taxAmount");
  var totTaxEarn = document.getElementById("totalTaxedEarnings");
  var taxRt = document.getElementById("taxRate");

  console.log(amount);
  console.log(taxRate);

  if (!isNaN(amount))
  {
    targAmt.innerHTML = amount;
    totTaxEarn.innerHTML = amount / taxRate;
    taxRt.innerHTML = ta开发者_C百科xRate;
    taxAmt.innerHTML = (amount / taxRate) - amount;
  }
}

guess where it explodes? taxRate is not visible from within this function. i thought that by setting it with the onChange of the radio button, this would make it accessible... but i guess not. how can i get that value into the variable such that i can be used in the doTheMath(); function?

thanks for your time. srsly. :)

WR!

PS: and this time, i'm unlikely to figure it out on my own... i've spent three hours on this one... :P


The problem is in following code:

    <input type="radio" name="B" id="B_1" value="200" onChange="var taxRate = (this.value);">200

taxRate is declared with 'var' keyword makes it is visible on 'onChange' event only. Please remove 'var' keyword.


Skip the 'onchange' handler, and just get the currently selected value in doTheMath. I would do it with jQuery this way:

$('radio[name=B]:checked').val()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜