【问题标题】:Passing variable amount to Paypal donation button not working将可变金额传递给 Paypal 捐赠按钮不起作用
【发布时间】:2015-11-22 12:30:16
【问题描述】:

我已尝试将派生的总数传递给贝宝捐赠按钮(非托管按钮),但贝宝仍显示 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>

【问题讨论】:

    标签: javascript button paypal


    【解决方案1】:

    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;
    }               
    

    【讨论】:

    • 非常感谢保罗。我创建了新的 ID,它起作用了。
    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 2013-07-27
    • 2014-10-24
    • 2015-08-17
    • 2013-07-20
    • 2016-06-27
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多