WooCommerce 购物车页面更新后循环功能不 运行
Loop function not running after WooCommerce cart page update
我正在尝试 运行 WooCommerce 购物车页面上的一个简单循环。
当页面最初加载时,该函数执行它的事情,但在更新购物车时,它变得毫无生气。
这是我的循环函数:
let radios = document.querySelectorAll( 'input[name="radio_options_set"]' );
simplySetAttrs();
function simplySetAttrs() {
// I started the var at 1 to skip the first radio input
for (var xx = 1; xx < radios.length; xx++) {
radios[xx].setAttribute( 'onclick', 'dothisFunction( this );' );
}
}
唯一显示任何响应的 WooCommerce 事件是 'updated_cart_totals',我在购物车页面上定位的单选按钮由另一个插件插入。
以下是我的实现方式:
jQuery( document.body ).on( 'updated_cart_totals', simplySetAttrs() );
我不明白为什么最基本的循环没有采取行动。如果我执行 alert(),它会按预期工作,即使在设置超时后也是如此。
我也尝试添加不同的事件侦听器作为设置属性的替代方法,但无济于事。
您可以使用jQuery attr
来设置属性。试试下面的代码。
let radios = document.querySelectorAll( 'input[name="convert_to_sub"]' );
simplySetAttrs();
function simplySetAttrs() {
// I started the var at 1 to skip the first radio input
for (var xx = 1; xx < radios.length; xx++) {
jQuery(radios[xx]).attr( 'onclick', 'dothisFunction( this );' );
}
}
我正在尝试 运行 WooCommerce 购物车页面上的一个简单循环。 当页面最初加载时,该函数执行它的事情,但在更新购物车时,它变得毫无生气。
这是我的循环函数:
let radios = document.querySelectorAll( 'input[name="radio_options_set"]' );
simplySetAttrs();
function simplySetAttrs() {
// I started the var at 1 to skip the first radio input
for (var xx = 1; xx < radios.length; xx++) {
radios[xx].setAttribute( 'onclick', 'dothisFunction( this );' );
}
}
唯一显示任何响应的 WooCommerce 事件是 'updated_cart_totals',我在购物车页面上定位的单选按钮由另一个插件插入。
以下是我的实现方式:
jQuery( document.body ).on( 'updated_cart_totals', simplySetAttrs() );
我不明白为什么最基本的循环没有采取行动。如果我执行 alert(),它会按预期工作,即使在设置超时后也是如此。
我也尝试添加不同的事件侦听器作为设置属性的替代方法,但无济于事。
您可以使用jQuery attr
来设置属性。试试下面的代码。
let radios = document.querySelectorAll( 'input[name="convert_to_sub"]' );
simplySetAttrs();
function simplySetAttrs() {
// I started the var at 1 to skip the first radio input
for (var xx = 1; xx < radios.length; xx++) {
jQuery(radios[xx]).attr( 'onclick', 'dothisFunction( this );' );
}
}