简体   繁体   English

单选按钮管理 - 当我选择一个时,它会自动选择另一个 jQuery

[英]Radio button management - when I choose one, it automatically selects the other jQuery

I have 4 radio buttons, I would like when I click one (or set it to automatically select on start) so that when I select DeliveryYesSingle , it will automatically select deliveryYesSingleBoard .我有 4 个单选按钮,我希望当我单击一个时(或在开始时将其设置为自动 select),这样当我 select DeliveryYesSingle时,它会自动 select deliveryYesSingleBoard however this function doesn't work like in If choose "if selected"然而,这个 function 不像在 If choose "if selected" 中那样工作

jQuery: jQuery:

*
            if(jQ('[id="DeliveryYesSingle"]').is(':checked')){
                jQ('[id="deliveryYesSingleBoard"]').prop('checked', true);
            
    };

    if(jQ('[id="DeliveryNoSingle"]').is(':checked')){
        jQ('[id="DeliveryNoSingleBoard"]').prop('checked', true);
};

HTML: HTML:


                <div class="row gutters">
                    <div class="col col-2">
                        <div class="form-item form-checkboxes">
                            <input type="radio"   id="DeliveryYesSingle" name="deliveryCredit"   value="yes"/><label data-timtranslationlabel="" for="deliveryCredit_Yes" class="checkbox" >Delivery on credit</label>
                        </div>
                    </div>
                    <div class="col col-2">
                        <div class="form-item form-checkboxes">
                            <input type="radio" id="DeliveryNoSingle" name="deliveryCredit" value="no"/><label data-timtranslationlabel="" for="deliveryCredit_No" class="checkbox" >Advance payment to supplier</label>
                        </div>
                    </div>
                </div>
                                <div class="section" id="DeliveryOnCreditBoard" >    
                    <div class="row gutters">
                        <div class="col col-2">
                            <div class="form-item form-checkboxes">
                                <input type="radio" id="deliveryYesSingleBoard" name="deliverySingle"   value="yes"/><label data-timtranslationlabel="" for="deliveryYesSingleBoard" class="checkbox" >Delivery on credit</label>
                            </div>
                        </div>
                        <div class="col col-2">
                            <div class="form-item form-checkboxes">
                                <input type="radio"  id="DeliveryNoSingleBoard" name="deliverySingle"  value="np"/><label data-timtranslationlabel="" for="DeliveryNoSingleBoard" class="checkbox" >Advance payment to supplier</label>
                            </div>
                        </div>
                    </div>
                    </div>

https://jsfiddle.net/Palucci92/w4djnhgb/3/ https://jsfiddle.net/Palucci92/w4djnhgb/3/

Those two if statements will run immediately after the page loads, and only once.这两个if语句将在页面加载后立即运行,并且只会运行一次。 if statements should not be used to check for user input, such as selecting a radio button. if语句不应用于检查用户输入,例如选择单选按钮。 You'll need to register an event on the two radio buttons you want to check for user input on, for instance:您需要在要检查用户输入的两个单选按钮上注册一个事件,例如:

jQ('[id="DeliveryYesSingle"]').on('change', () => {

});

In this case, the change event is used to detect when a radio button is checked (see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event ).在这种情况下, change事件用于检测单选按钮何时被选中(参见https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event )。

And within the callback function block (inside the curly braces), you can then perform your .prop() call to change the property of the other radio buttons.在回调 function 块中(在大括号内),您可以执行.prop()调用来更改其他单选按钮的属性。

暂无
暂无

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

相关问题 当我尝试单击一个单选按钮时,它将选择另一个 - When I try to click one radio button it selects the other 当选中一个单选按钮时,应自动选中另一个单选按钮 - when one radio button is checked the other radio button should automatically get checked jQuery代码,单击单选按钮选择一个选项 - Jquery code, clicking on radio button selects an option 当我单击单选按钮时,它的true或自动其他Span值将变为false - When i click on radio button then its true or automatically other Span value goes to false Reactjs:我正在检查基于 state 的单选按钮,但在选中一个单选按钮时无法单击其他单选按钮 - Reactjs: I am checking radio button based on state but can't click other radio button when one is checked 克隆包含单选按钮的div时,jQuery自动为单选按钮名称添加+1 - jQuery automatically add +1 to the radio button name when the div containing the radio button is cloned 我有两个单选按钮,但即使我们选择不同的收音机,表格也只能获得一个收音机值 - I have two radio button but the the form only can get one radio value even we choose different radio 启用一个无线电组的单选按钮,并在选中时禁用另一个无线电组 - To enable Radio button of one radio group and disable the other radio group when selected 当用户选择单选按钮时如何显示不同的内容? - How to display different content when a user selects a radio button? 为什么当label值不同时单选按钮选择多个选项 - Why Radio button selects multiple options when label values are different
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM