wordpress - How to clear form input on page load woocommerce checkout
问题描述
I am offering customers a repurchase on the order confirmed page. If they click the link they are sended directly to the checkout page. However, the form is then prefilled with the previous data. I would like to clear it, because the idea is one product(ticket) per person.
So far i have the following (and in the page source i can see the script is loaded):
function print_script() {
if (isset($_GET['extrapersoon']) && is_page(21560)){
echo '<script>
window.addEventListener("load", function(){myFunction()};
function myFunction() {
document.getElementsByClassName("woocommerce-checkout").reset();
}
</script>';
}
}
add_action('wp_print_scripts', 'print_script');
I tried other triggers too, such as window.load but no result yet.
Anyone?
解决方案
You should put the script in the footer using add_action( 'wp_footer', 'print_script' );
to make sure the form gets cleared after the data was loaded into it.
Wordpress has jQuery on default, so I'm using it to look for all the forms in the document and clear them. The function is automatically called on page load, therefore no need for a event listener.
<?php
function print_script() {
if (isset($_GET['extrapersoon']) && is_page(21560)) { ?>
<script type="text/javascript">
( function( $ ) {
$('form').each(function() { this.reset() });
}( jQuery ) );
</script>
<?php }
}
add_action( 'wp_footer', 'print_script' ); ?>
推荐阅读
- python - 无法弹出网页(python、selenium)
- git - Git 日志多行输出
- ubuntu - 为函数添加了函数参数
- python - 使用 nf 过滤器在 mininet 中的主机充当 mininet SDN 模拟中的代理
- toit - 如何重置 PubSub 中未确认的消息?
- google-cloud-platform - 谷歌云中有没有办法阻止访问特定网址的尝试
- python - sort_values() 与 'key' 对数据框中的一列元组进行排序
- pytorch - 在 pytorch 强化学习中更改输入类型的问题
- linux - 如何独立于扬声器控制键盘上的静音 LED
- ios - 如何使用 iOS 共享扩展将 pdf 文件从电子邮件/iOS 设备共享到 iOS 应用程序?