简体   繁体   English

更改单选按钮并单击以设置其样式

[英]Change radio button and style it with click

I have created HTML for changing payment method. 我已经创建了用于更改付款方式的HTML。

<ul id="checkout-method">
    <li><input name="payment_method" type="radio" value="Cash on Delivery">
        <label>Cash on Delivery</label>
        <p class="explainpaymethod" style="display:none">Pay with cash upon delivery.</p>
    </li>
    <li><input name="payment_method" type="radio" value="Paypal">
        <label>Paypal</label>
        <p class="explainpaymethod" style="display:none">Pay via PayPal; you can pay with your credit card if you don’t have a PayPal account.</p>
    </li>
</ul>

But I do not know how to do : 但是我不知道该怎么办:

  • Always make first input is checked. 始终检查第一个input

  • Click on li parent tag to check radio instead of radio button. 单击li parent标签以检查单选而不是单选按钮。

  • If one is check, .explainpaymethod will show, other is hide. 如果一个被选中, .explainpaymethod将显示,另一个被隐藏。

Thank you very much. 非常感谢你。

  • To make the first radio checked, use checked property in the first input tag. 要检查第一个无线电,请在第一个输入标签中使用checked属性。

  • To be able to click on li parent tag, check radio instead, use display: block; 为了能够点击li父标签,检查无线电而是使用display: block; property on <label> <label>属性

You can achieve all your properties like this: 您可以像这样实现所有属性:

 $(document).ready(function(){ $("input[type='radio']").on('click', function(e) { $('p.explainpaymethod').hide(); if($(this).is(':checked')) { $(this).closest('li').find('p.explainpaymethod').show(); } }) }); 
 label { display: block; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="checkout-method"> <li><label><input name="payment_method" type="radio" value="Cash on Delivery" checked> Cash on Delivery</label> <p class="explainpaymethod" style="display:inline-block">[Pay with cash upon delivery.]</p> </li> <li><label><input name="payment_method" type="radio" value="Paypal"> Paypal</label> <p class="explainpaymethod" style="display:none">[Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.]</p> </li> </ul> 

Hope this helps! 希望这可以帮助!

Always make first input is checked. 始终检查第一个输入。

Use the checked attribute in the HTML to set the default selected option. 使用HTML中的checked属性来设置默认的选定选项。

Click on li parent tag to check radio instead of radio button. 单击li parent标签以检查单选而不是单选按钮。

Don't add the click on the li , instead wrap the input and the text in the <label> element. 不要在li上添加单击,而是将输入和文本包装在<label>元素中。 This is a standard control which expands the hit area for the radio button. 这是一个标准控件,可扩展单选按钮的点击区域。

If one is checked, .explainpaymethod will show, other is hide. 如果选中其中一个,则将显示.explainpaymethod,其他则为隐藏。

You will need some JS to do this. 您将需要一些JS来执行此操作。 You need to hide all the .explainpaymethod elements before showing the one related to the changed radio button by using DOM traversal techniques. 在使用DOM遍历技术显示与已更改的单选按钮相关的元素之前,您需要隐藏所有.explainpaymethod元素。 Specifically closest() and find() . 特别是closest()find() Try this: 尝试这个:

 $('#checkout-method input').change(function() { $('.explainpaymethod').hide(); $(this).closest('li').find('.explainpaymethod').show(); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="checkout-method"> <li> <label> <input name="payment_method" type="radio" value="Cash on Delivery" checked="true"> Cash on Delivery </label> <p class="explainpaymethod">Pay with cash upon delivery.</p> </li> <li> <label> <input name="payment_method" type="radio" value="Paypal"> Paypal </label> <p class="explainpaymethod" style="display:none">Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.</p> </li> </ul> 

1)Always make first "input" is checked. 1)始终先检查“输入”。

$('input[type="radio"]').eq(0).prop('checked',true);

2)Click on li parent tag to check radio instead of radio button. 2)单击父标签来检查单选而不是单选按钮。

You need to use <label for="parent_id"> 您需要使用<label for="parent_id">

<label for="Paypal">Paypal</label>

3)If one is check, .explainpaymethod will show, other is hide. 3)如果一个被检查,将显示.explainpaymethod ,另一个被隐藏。

$('input[type="radio"]').change(function() {
   $('.explainpaymethod').hide();
   $(this).closest('li').find('.explainpaymethod').show();
});

 $('input[type="radio"]').eq(0).prop('checked',true); $('input[type="radio"]').eq(0).closest('li').find('.explainpaymethod').show(); $('input[type="radio"]').change(function() { $('.explainpaymethod').hide(); $(this).closest('li').find('.explainpaymethod').show(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="checkout-method"> <li><input name="payment_method" type="radio" value="Cash on Delivery" id="Cash"> <label for="Cash">Cash on Delivery</label> <p class="explainpaymethod" style="display:none">Pay with cash upon delivery.</p> </li> <li><input name="payment_method" type="radio" value="Paypal" id="Paypal"> <label for="Paypal">Paypal</label> <p class="explainpaymethod" style="display:none">Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.</p> </li> </ul> 

You can do it without javascript. 您无需使用javascript就可以做到。

First add checked attribute to the input which you want to be checked by default. 首先,将checked属性添加到您要默认检查的输入中。

Then add "id" attribute to the inputs and "for" attribute to the labels. 然后将“ id”属性添加到输入,并将“ for”属性添加到标签。

<ul id="checkout-method">
    <li><input name="payment_method" type="radio" checked value="Cash on Delivery" id="cod">
        <label for="cod">Cash on Delivery</label>
        <br>
        <p class="explainpaymethod">Pay with cash upon delivery.</p>
    </li>
    <li><input name="payment_method" type="radio" value="Paypal" id="pp">
        <label for="pp">Paypal</label>
        <br>
        <p class="explainpaymethod">Pay via PayPal; you can pay with your credit card if you don’t have a PayPal account.</p>
    </li>
</ul>

Then add the following CSS which shows paragraph if their sibling input is checked. 然后添加以下CSS,如果选中了同级输入,则显示段落。

.explainpaymethod {
  display: none;
}
input:checked ~ .explainpaymethod {
  display: inline-block;
}

Here is the link to the fiddle: https://jsfiddle.net/u1zjyorf/3/ 这是小提琴的链接: https : //jsfiddle.net/u1zjyorf/3/

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

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