简体   繁体   中英

JQuery .removeAttr('required') not working for input radio group

What I am trying to do is when $('input:radio[name="NeverWornRespbefore"]') is selected, I want to remove the required attribute from the radio group $('input:radio[name="eyeIrritation"]') .

My question : How come the .removeAttr('required') or any other of the commented sections in the code won't remove the required tag? How do i get this to work?

Thank you.

<script>
//If user presses number 8 these radios don't have to be required
jQuery(document).ready(function () {

    $('input:radio[name="NeverWornRespbefore"]').change(function () {
        if ($(this).val() == 'Yes Never Worn one before') {

            console.log("triggered");
            //$('input:radio[name="eyeIrritation"]').removeAttr('required');​​​​​
            //$('.eyeIrritation').each().attr("required", "false");
            $('.eyeIrritation').removeAttr('required');
            console.log($('input#eyeIrritation'));
        }
    });

});

HERE is my cshtml:

<tr>
                    <td><label class="col-xs-12 col-sm-6 col-md-8 control-label" for="employeeName">8. If you've used a respirator, have you ever had any of the following problems? </label><be><label class="col-xs-12 col-sm-6 col-md-8 control-label" for="employeeName">(If you've never used a respirator, check the following checkbox @Html.RadioButtonFor(m => m.NeverWornRespbefore, "Yes Never Worn one before", new { Value = "Yes Never Worn one before" }) and go to question 9)</label></td>
                    <td>Yes</td>
                    <td>No</td>
                </tr>
                <tr>
                    <td><label class="col-xs-12 col-sm-6 col-md-8 control-label" for="employeeName">a. eye irritation:</label></td>
                    <td>@Html.RadioButtonFor(m => m.eyeIrritation, "Yes", new {  Value = "YES" , required = "true"})</td>
                    <td>@Html.RadioButtonFor(m => m.eyeIrritation, "No", new { Value = "NO"  , required = "true"})</td>
                </tr>

HERE IS MY COMPLETED CODE With ANSWER found from COMMENTS

<script>
//If user presses number 8 these radios don't have to be required
jQuery(document).ready(function () {

    $('input:radio[name="NeverWornRespbefore"]').change(function () {
        if ($(this).val() == 'Yes Never Worn one before') {

            console.log("Number never worn resp triggered");

            $('input[name="eyeIrritation"]').removeAttr('required');
        }
    });

});

I had the wrong selector for the radio group.

怎么样

.prop('required', false);

Your selector is wrong. Try $('input[name="eyeIrritation"]').removeAttr('required');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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