简体   繁体   English

默认情况下无法预先选择所需的单选框

[英]Not being able to pre-select desired radio box by default

I am trying to pre-select premium delivery by default. 我尝试默认情况下预选高级投放。 I was looking on the web and really don't understand why it would not pre-select the second radio-box. 我在网上浏览,真的不明白为什么它不能预选第二个收音机盒。 Please find link to my JSfiddle 请找到我的JSfiddle的链接

My code is also: 我的代码也是:

 jQuery(document).ready(function() { waitForDelayedContent('#checkout-shipping-method-load .input-checkout-radio .method-title:contains(Take it to my room)', function() { jQuery('#checkout-shipping-method-load .input-checkout-radio:not(.mtC) .method-title:contains(Take it to my room)').click(); jQuery('#checkout-shipping-method-load .input-checkout-radio:not(.mtC):has(.method-title:contains(Take it to my room)) .radio').click(); jQuery('#checkout-shipping-method-load .input-checkout-radio:has(.method-title:contains(Take it to my room))').addClass('mtC'); }); }); 
 <div id="checkout-shipping-method-load"> <div class="sp-methods"> <h3 class="title">Delivery Option</h3> <p>You must select a delivery option.</p> <ul> <li class="delivery-method"> <div class="input-checkout-radio"> <input checked="checked" class="input-radio" id="s_method_standard" name="shipping_method" type="radio" value="paragon_customrate_standard"> <label class="radio-label" for="s_method_standard"><span class="radio"></span> <span class="method-title">FREE Take it to my door</span> </label> </div> </li> <li class="delivery-method"> <div class="input-checkout-radio"> <input class="input-radio" id="s_method_premium" name="shipping_method" type="radio" value="paragon_customrate_premium"> <label class="radio-label" for="s_method_premium"><span class="radio"></span> <span class="method-title"><span class= "price"><span class="currency">£</span>39</span>Take it to my room</span> </label> </div> </li> </ul> </div> </div> 

It is because the first radio box has checked="check" . 这是因为第一个单选框已checked="check" Move that to the second radio box to make it work. 将其移动到第二个单选框以使其正常工作。 No need for JavaScript. 无需JavaScript。 See this updated fiddle: http://jsfiddle.net/n5h65n73/ 看到这个更新的小提琴: http : //jsfiddle.net/n5h65n73/

Or, if you really need to do it with JavaScript: 或者,如果您确实需要使用JavaScript:

$("#s_method_premium").prop("checked", true)

The issue is within your HTML. 问题出在您的HTML中。 You have the checked="checked" attribute set on the first radio input. 您已在第一个单选输入上设置了checked="checked"属性。 So if you remove that attribute and move it to the second one, it'll work as you want. 因此,如果您删除该属性并将其移至第二个属性,它将可以根据需要工作。

 <div id="checkout-shipping-method-load"> <div class="sp-methods"> <h3 class="title">Delivery Option</h3> <p>You must select a delivery option.</p> <ul> <li class="delivery-method"> <div class="input-checkout-radio"> <input class="input-radio" id="s_method_standard" name="shipping_method" type="radio" value="paragon_customrate_standard"> <label class="radio-label" for="s_method_standard"><span class="radio"></span> <span class="method-title">FREE Take it to my door</span> </label> </div> </li> <li class="delivery-method"> <div class="input-checkout-radio"> <input checked class="input-radio" id="s_method_premium" name="shipping_method" type="radio" value="paragon_customrate_premium"> <label class="radio-label" for="s_method_premium"><span class="radio"></span> <span class="method-title"><span class= "price"><span class="currency">£</span>39</span>Take it to my room</span> </label> </div> 

To do this using jQuery, here's the code snippet: 要使用jQuery做到这一点,下面是代码片段:

 $('#s_method_premium').attr('checked', 'true'); 

The basic explanation is that you are using the attr method of jQuery to modify the property (ie, the first argument) with the desired value (ie, the second argument). 基本的解释是您正在使用jQuery的attr方法将属性(即第一个参数)修改为所需的值(即第二个参数)。 And then necessity for both lines of code is to remove the first checked before setting the second one. 然后,这两行代码都必须在设置第二条代码之前先删除第一条代码。

Does that help? 有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM