Wooccommerce单选按钮检查jquery
Woocommerce radio button check on jquery
我在WooCommerce上有一个自定义运输方法,只有当选择了该自定义运输方法时,它才会向用户显示一个包含信息的小框。
到目前为止,我已经尝试了这个小代码:
jQuery(document).ready(function($) {
$(document).on("change", "#shipping_method_0_my_shipping_method", function() {
if( $("#shipping_method_0_my_shipping_method").prop('checked') == true ){
$('.custom-box').show();
} else {
$('.custom-box').hide();
}
});
});
当选择自定义运输方式时,它会显示.custom框。但它并没有因为某种原因而隐藏它,当我选择其他运输方式时?
此外,如果在页面加载时选择了我的自定义运输方法,它不会显示框,但如果我单击其他运输方法,然后单击我的自定义运送方法,它会显示框。
现在工作得很好。感谢以下ТомицаКора的工作解决方案!
试试这个脚本:
jQuery(document).ready(function($) {
// Create reusable function to show or hide .custom-box
function toggleCustomBox() {
// Get id of selected input
var selectedMethod = $('input:checked', '#shipping_method').attr('id');
// Toggle .custom-box depending on the selected input's id
// Replace 'shipping_method_0_my_shipping_method' with your desired id
if (selectedMethod === 'shipping_method_0_my_shipping_method') {
$('.custom-box').show();
} else {
$('.custom-box').hide();
};
};
// Fire our function on page load
$(document).ready(toggleCustomBox);
// Fire our function on radio button change
$('#shipping_method input:radio').change(toggleCustomBox);
});
让我知道这是否适合你。
编辑
我已经更新了脚本。请尝试一下,让我知道它是否有效:
jQuery(document).ready(function($) {
// Create reusable function to show or hide .custom-box
function toggleCustomBox() {
// Get id of selected input
var selectedMethod = $('input:checked', '#shipping_method').attr('id');
// Toggle .custom-box depending on the selected input's id
// Replace 'shipping_method_0_my_shipping_method' with your desired id
if (selectedMethod === 'shipping_method_0_my_shipping_method') {
$('.custom-box').show();
} else {
$('.custom-box').hide();
};
};
// Fire our function on page load
$(document).ready(toggleCustomBox);
// Fire our function on radio button change
$(document).on('change', '#shipping_method input:radio', toggleCustomBox);
});
相关文章:
- 使用 jQuery 检查所有值是否为空或已填充
- 使用jQuery检查提交时添加到句子中的单词
- 使用 jQuery 检查元素的数量
- 使用js/jQuery检查对象(而不是元素)是否真的存在
- jquery检查数字是否键入了isNaN
- 如何使用JQuery检查输入(电子邮件)字段是否包含特殊字符
- 使用javascript/jquery检查.css样式表的名称
- 使用JQuery检查数组的索引中是否存在字符串
- jQuery检查CSS宽度是否大于0,然后添加CSS属性
- 使用jQuery检查文件是否存在
- jQuery检查字符串前面是否有其他字母
- 使用 jQuery 检查文本可见性 .包含代码的行为不符合预期
- 使用 jQuery 检查文本框焦点
- 如何使用jQuery检查文本框中的文本是否 asp.net 选中
- 如何使用 jquery 检查该值是否存在于带有标签的 select2 下拉列表中:true
- jQuery检查自定义Google电子表格页面上输入和下拉列表的更改
- jQuery检查元素是否有类
- jQuery检查URL,然后显示和隐藏元素
- jQuery检查按下的键是否可打印
- javascript,jQuery-检查数组中是否存在值