简体   繁体   English

一次只能选择一个单选按钮

[英]Only one radio button must be selected at a time

I want to select one radio button out of two radio button using Javascript here is my html code I have我想使用Javascript从两个单选按钮中选择一个单选按钮这里是我的html代码

<div class="row">
    <div class="form-group">
        <label class="control-label col-sm-6">Feel Of The Cards : </label>
        <div class="col-sm-6">
            <input type="radio" value="NonEmbossed" name="feel_of_card" /> Non Embossed/Smooth Finished<br>
            <input type="radio" value="Embossed" name="feel_of_card1" /> Embossed/Linen Finished/Textured Cards
        </div>
    </div>
</div>

I want to make a separate function in which a one radio button will be selected out of two radio button?我想创建一个单独的功能,从两个单选按钮中选择一个单选按钮?

what is the possible way to write javascript function ?编写javascript函数的可能方法是什么?

You don't need JavaScript to do that.你不需要 JavaScript 来做到这一点。 Just give same name to both checkboxes只需为两个复选框指定相同的name

 <div class="row"> <div class="form-group"> <label class="control-label col-sm-6">Feel Of The Cards : </label> <div class="col-sm-6"> <input type="radio" value="NonEmbossed" name="feel_of_card" /> Non Embossed/Smooth Finished<br /> <input type="radio" value="Embossed" name="feel_of_card" /> Embossed/Linen Finished/Textured Cards </div> </div> </div>

<div class="row">
                        <div class="form-group">
                        <label class="control-label col-sm-6">Feel Of The Cards : </label>
                        <div class="col-sm-6">
                        <input id="_1234"  type="radio" value="NonEmbossed" name="feel_of_card" /> Non Embossed/Smooth Finished<br>
                        <input id="_1235" type="radio" value="Embossed" name="feel_of_card" /> Embossed/Linen Finished/Textured Cards</div>
                        </div>
                        </div>

Why do you use all instead of id value?为什么使用 all 而不是 id 值? Also do not mix CSS syntax (# for identifier) with native JS Native JS solution:也不要将 CSS 语法(# 表示标识符)与原生 JS 原生 JS 解决方案混合使用:

document.getElementById("_1234").checked = true; document.getElementById("_1234").checked = true;

JQuery solution: jQuery 解决方案:

$("#_1234").prop("checked", true); $("#_1234").prop("checked", true);

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

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