有没有办法根据 link 自动填充表单字段?

Is there a way to autofill a form field based on a link?

我试图让按钮 link 在单击时将特定代码生成到表单字段中,但似乎找不到任何有效的解决方案。我知道必须有一个 Javacript 答案,但是 none 的建议解决方案正在完成。

表单代码:

<div class="nm-checkout-form-coupon">
        <form class="checkout_coupon" method="post" style="display:none">
            <p class="form-row">
                <input type="text" id="coupon_code" name="coupon_code" class="input-text" placeholder="<?php _e( 'Physician Code', 'woocommerce' ); ?>" id="coupon_code" value="" required />
            </p>
            <p class="for-row">
                <p>Don't have a Physician Code? <a href="" data-toggle="modal" data-target="#physcode">Get one here!</a></p>
                <!-- Physician Code Modal -->
                    <div class="modal fade access-code-popup" id="physcode" tabindex="-1" role="dialog" aria-labelledby="physcodelabel" >
                  <div class="modal-dialog">
                   <div class="modal-content">
                    <div class="modal-body">
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                      <p>AQ Skin Pro products are available exclusively through participating clinics and physicians offices. If you do not have an Exclusive Physician Code, click "generate a code" below.</p>
                      <button class="nm-simple-add-to-cart-button single_add_to_cart_button button alt" data-dismiss="modal" aria-label="Close" onClick="fillFields('coupon_code')" alutocomplete="off">Generate a Code</button>
                      </div>
                    </div>
                   </div>
                </div>
            </p>
            <p class="form-row">
                <input type="submit" class="button" name="apply_coupon" value="<?php _e( 'Apply Physician Code', 'woocommerce' ); ?>" />
            </p>
        </form>
    </div>

您当前的尝试距离不远:<button class="nm-simple-add-to-cart-button single_add_to_cart_button button alt" data-dismiss="modal" aria-label="Close" onclick="fillFields('coupon_code')" alutocomplete="off" id="gencode">Generate a Code</button>

如果您想保持简单 将 onclick 代码替换 为: document.getElementById('coupon_code').value = 'THECOUPONCODE';

你应该得到这样的结果:<button class="nm-simple-add-to-cart-button single_add_to_cart_button button alt" data-dismiss="modal" aria-label="Close" onclick="document.getElementById('coupon_code').value = 'THECOUPONCODE';" alutocomplete="off" id="gencode">Generate a Code</button>