[英]JQuery conflict (maybe?) Hide/Show Div based on Radio Box
This is driving me crazy. 这真让我抓狂。 I have tried every different ways to do this.
我尝试了各种不同的方法来做到这一点。 Trying to show/hive a div based on a radio box selection.
尝试根据单选框显示/显示div。 Below is my code into my main.js :
下面是我进入main.js的代码:
$("input[name='payment_method']").click(function () {
if ($(this).attr("id") == "payment_method_cod") {
$("#checkout_insurance").show();
} else {
$("#checkout_insurance").hide();
}
});
HTML: HTML:
<div id="checkout_insurance">
<h2>Checkout with Insurance</h2>
</div>
<div id="payment" class="woocommerce-checkout-payment">
<ul class="payment_methods methods">
<li class="payment_method_cod">
<input id="payment_method_cod" type="radio" class="input-radio" name="payment_method" value="cod" data-order_button_text="" checked="checked">
<label for="payment_method_cod">
...
</li>
<li class="payment_method_firstdata">
<input id="payment_method_firstdata" type="radio" class="input-radio" name="payment_method" value="firstdata" data-order_button_text="">
<label for="payment_method_firstdata">
....
</li>
</ul>
</div>
I cant get to work. 我不能上班。 If I place another cdn of the jQuery on the footer, the function works, however messes up a lot of other stuff.
如果我在页脚上放置另一个jQuery的CDN,则该函数有效,但是会弄乱很多其他内容。 jQuery is being loaded on the header.
jQuery正在头文件中加载。 I am current using jQuery 1.11.3.
我目前正在使用jQuery 1.11.3。
Any help will be appreciated. 任何帮助将不胜感激。 Thank you!
谢谢!
Use :checked
selector to check if the radio element with id payment_method_cod
is checked or not. 使用
:checked
选择器可以检查是否已选中ID为payment_method_cod
的单选元素。
$('#payment_method_cod:checked')
will return true/false
based on the condition if the radio button is checked or not and toggle
it, when true
, the element #checkout_insurance
will be shown. $('#payment_method_cod:checked')
将根据条件返回true/false
,如果选中或不选中单选按钮并toggle
它, #checkout_insurance
true
,将显示元素#checkout_insurance
。 Assuming the id's used in the code are unique. 假设代码中使用的ID是唯一的。
Demo 演示
$(document).on('change', "input[name='payment_method']", function() { $("#checkout_insurance").toggle($('#payment_method_cod').is(':checked')); }).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="checkout_insurance"> <h2>Checkout with Insurance</h2> </div> <div id="payment" class="woocommerce-checkout-payment"> <ul class="payment_methods methods"> <li class="payment_method_cod"> <input id="payment_method_cod" type="radio" class="input-radio" name="payment_method" value="cod" data-order_button_text="" checked="checked"> <label for="payment_method_cod">Payment</label> ... </li> <li class="payment_method_firstdata"> <input id="payment_method_firstdata" type="radio" class="input-radio" name="payment_method" value="firstdata" data-order_button_text=""> <label for="payment_method_firstdata">FirstData</label> .... </li> </ul> </div>
$("input[name='payment_method']").change(function () {
if ($(this).attr("id") == "payment_method_cod" && $(this).is(':checked')) {
alert('show');
} else {
alert('hide');
}
});
Please use .change()
on checkbox use .click()
on button. 请在复选框上使用
.change()
.click()
在按钮上使用.change()
。 Try this approach this will select change event on checkbox and check if the id of the checkbox is payment_method_cod
it will then show or hide depending on checked property 尝试这种方法,这将选择复选框上的更改事件,并检查复选框的ID是否为
payment_method_cod
,然后它将根据选中的属性显示或隐藏
Changing your code a bit : 稍微修改一下代码:
$("input[name='payment_method']").click(function () {
var _this = $(this);
if (_this.val() == "cod") {
$("#checkout_insurance").show();
} else {
$("#checkout_insurance").hide();
}
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.