简体   繁体   English

jQuery:无法检查是否选择了单选按钮

[英]jQuery: Cannot check if radio button is selected

I have the following form: 我有以下表格:

    <div class="form-radios">

    <!-- OPTION #1 -->

    <div class="form-item form-type-radio">             
        <input type="radio" name="auswahl_radio" id="option1" class="form-radio">  <label class="option" for="option1">Option 1</label>

        <div class="radio-supplement">
            <p>Please accept:</p>
            <input type="checkbox" id="checkbox_1" value="beer"><a href="#">Checkbox 1</a>
            <input type="checkbox" id="checkbox_2" value="wine"><a href="#">Checkbox 2</a>
        </div>      

    </div>


    <!-- OPTION #2 -->

    <div class="form-item form-type-radio">
        <input type="radio" name="auswahl_radio" id="option2" class="form-radio">  <label class="option" for="option2">Option 2 </label>
    </div>


    <!-- OPTION #3 -->

    <div class="form-item form-type-radio">
        <input type="radio" name="auswahl_radio" id="option3" class="form-radio">  <label class="option" for="option3">Option 3</label>
    </div>

    </div>

What I want to achieve is that when the user clicks the submit-button and checked OPTION #1, then my script shall check if both checkboxes in the containing div.radio-supplement are checked. 我要实现的是, 当用户单击“提交”按钮并选中“选项1”时,我的脚本将检查包含div.radio-supplement中的两个复选框是否都被选中。 If not at least one of them is checked, the form must not be sent. 如果未选中其中至少一项,则不得发送表格。

Here is the relevant jQuery script I came up with but doesn't work: 这是我想出的相关jQuery脚本,但不起作用:

var checkbox_1 = $('#checkbox_1').prop('checked');  
var checkbox_2 = $('#checkbox_2').prop('checked');

     if ($("#option1").is(':checked')) {

         if ( (checkbox_1 == true) && (checkbox_2 == true) ) {
                return true;
            } else {
                $('.messages_error').html('At option1 please check both checkboxes');
                window.scrollTo(0,0);
                return false;
         };

     };

Does anyone of you see the error? 你们中有人看到错误吗?

Try with this: 试试这个:

var radios = jQuery("input[name='auswahl_radio']");
var radio_is_checked = radios.filter(":checked") ;

THEN check for this radio_is_checked variable. 然后检查此radio_is_checked变量。

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

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