将可变金额传递给Paypal捐款按钮不起作用

时间:2015-08-27 23:23:02

标签: javascript button paypal

我试图将派生的总额传递给paypal捐赠按钮(非托管按钮),但Paypal仍显示0.我尝试了我在此网站上阅读的所有内容,但仍无法使其正常工作。任何帮助表示赞赏。

这是我的脚本。

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode != 46 && charCode > 31
      && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function sum() {
    var txtPKDValue = document.getElementById('txtPKD').value;
    var txtHeartValue = document.getElementById('txtHeart').value;
    var txtAlzheimerValue = document.getElementById('txtAlzheimer').value;
    var txtDiabetesValue = document.getElementById('txtDiabetes').value;
    var txtStridesValue = document.getElementById('txtStrides').value;
    var txtNightWalkValue = document.getElementById('txtNightWalk').value;
    var txtJingleValue = document.getElementById('txtJingle').value;
    var txtMSValue = document.getElementById('txtMS').value;
    var txtDimesValue = document.getElementById('txtDimes').value;
    var txtStepsValue = document.getElementById('txtSteps').value;
    var txtUnspecifiedValue = document.getElementById('txtUnspecified').value;

    if (txtPKDValue == "")
        txtPKDValue = 0;
    if (txtHeartValue == "")
        txtHeartValue = 0;
    if (txtAlzheimerValue == "")
        txtAlzheimerValue = 0;
    if (txtDiabetesValue == "")
        txtDiabetesValue = 0;
    if (txtStridesValue == "")
        txtStridesValue = 0;
    if (txtNightWalkValue == "")
        txtNightWalkValue = 0;
    if (txtJingleValue == "")
        txtJingleValue = 0;
    if (txtMSValue == "")
        txtMSValue = 0;
    if (txtDimesValue == "")
        txtDimesValue = 0;
    if (txtStepsValue == "")
        txtStepsValue = 0;
    if (txtUnspecifiedValue == "")
        txtUnspecifiedValue = 0;

    var result = parseInt(txtPKDValue) + parseInt(txtHeartValue) + parseInt(txtAlzheimerValue) +
        parseInt(txtDiabetesValue) + parseInt(txtStridesValue) + parseInt(txtNightWalkValue) + parseInt(txtJingleValue) + parseInt(txtMSValue) + parseInt(txtDimesValue)
        + parseInt(txtStepsValue) + parseInt(txtUnspecifiedValue);
    if (!isNaN(result)) {
        document.getElementById('txtTotal').value = result;
    }
}
<table width="100%">
  <tr>
    <td align="center">
      <fieldset>
        <legend><strong>Donations</strong></legend>
        <table width="35%">
          <tr>
            <td><strong>Name</strong></td>
            <td>:</td>
            <td>
              <input id="txtName" type="text" /></td>
          </tr>
          <tr>
            <td style="font-weight: 700">E-Mail</td>
            <td>:</td>
            <td>
              <input id="txtMail" type="text" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-709251636882788888">
                Sacramento Walk for PKD <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtPKD"  type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-580604488995192577">
                Heart Walk <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtHeart" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-543781094323884944">
                Walk to End Alzheimer&#39;s <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtAlzheimer" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-150817808255225992">
                Walk to Cure Diabetes <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtDiabetes" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-235351783434196510">
                Making Strides of Sacramento <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtStrides" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-304111226551762696">
                Light the Night Walk <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtNightWalk" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-304111226551762696">
                Jingle Bell Run/Walk <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtJingle" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-478476217892767991">
                Walk MS <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtMS" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-843779283694541422">
                March of Dimes <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <input id="txtDimes" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-843779283694541422">
                <label class="wsite-form-label" for="input-229220521676593435">
                  Take Steps
                </label>
              </label>
            </td>
            <td>:</td>
            <td>
              <input id="txtSteps" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <strong>Unspecified</strong></td>
            <td>:</td>
            <td>
              <input id="txtUnspecified" type="text" onkeyup="sum()" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-843779283694541422">
                Total Amount</label>
            </td>
            <td>:</td>
            <td>
              <input id="txtTotal" type="text" readonly="true" onkeypress="return isNumberKey(event)" /></td>
          </tr>
          <tr>
            <td>
              <label class="wsite-form-label" for="input-277539319803365071">
                Comments <span class="form-not-required">*</span></label>
            </td>
            <td>:</td>
            <td>
              <textarea rows="5" id="txtComment" cols="20"></textarea></td>
          </tr>
          <tr>
            <td colspan="3" align="center">
      <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
        <input type="hidden" name="cmd" value="_donations">
        <input type="hidden" name="business" value="XXXXXXXXXXXXX">
        <input type="hidden" name="lc" value="US">
        <input type="hidden" name="item_name" value="GFF">
        <input type="hidden" name="item_number" value="GFF2015">
        <input type="hidden" name="no_note" value="0">
        <input type="hidden" name="cn" value="Add special instructions to the seller:">
        <input type="hidden" name="no_shipping" value="1">
        <input type="hidden" name="currency_code" value="USD">
        <input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHosted">
        <input type="hidden" name="amount" id="txtTotal" >
        <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
        <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
      </form>       
            </td>
          </tr>
        </table>
      </fieldset>
    </td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

您有id=txtTotal两个不同的元素。 ID必须是唯一的。任何代码设置txtTotal的值只会设置第一个找到的那个 - 这里的只读版本:

<td>
  <input id="txtTotal" type="text" readonly="true" onkeypress="return isNumberKey(event)" />
</td>

消除那一行,你应该处于更好的状态。或者给它一个新的ID,同时设置两个:

if (!isNaN(result)) {
  document.getElementById('txtTotal').value = result;
  document.getElementById('displayTxtTotal').value = result;
}