当选择单选按钮时,禁用需要消息

时间:2018-06-11 09:41:39

标签: javascript jquery

当我选中单选按钮时,我正在尝试禁用所需的消息"否" 但是,当我提交表单时,没有任何事情发生,它只是重定向到同一页面并显示所需的消息

更新了代码

//hide/show chapela  Yes/No
    $(document).ready(function () {
        $("input[name$='Chapel']").click(function () {
            var test = $(this).val();
            if (test == 'No') {
                $("div#hideChapel").hide();
            }
            else {
                $("div#hideChapel").show();
             }
        });
    });

    history.pushState(null, null, document.URL);
    window.addEventListener('popstate', function () {
        history.pushState(null, null, document.URL);
    });



    $(document).ready(function () {
        var test = $(this).val();
        if(test == 'No'){       
            $("hideChapel").prop("required", false);
        }
        else {
            $("hideChapel").prop("required", true);
        } 
    });
<div class="form-group">
  @Html.LabelFor(model => model.Chapel, htmlAttributes: new { @class = "control-label col-md-3" })
  <div class="col-md-9">
    <label class="radio-inline">
      @Html.RadioButtonFor(model => model.Chapel, "Yes", new {id="Yes", @class = "styled", htmlAttributes = new { @checked = "true"} })
      Yes
    </label>
    <label class="radio-inline">
      @Html.RadioButtonFor(model => model.Chapel, "No", new {id="No", @class = "styled", htmlAttributes = new { @checked = "true" } })
      No
    </label>
    @Html.ValidationMessageFor(model => model.Chapel, "", new { @class = "text-danger" })
  </div>
</div>

RENDERED HTML

<label class="radio-inline">
<div class="choice" id="uniform-No"><span class="checked"><input checked="checked" class="styled" htmlattributes="{ checked = true }" id="No" name="Chapel" type="radio" value="No" autocomplete="off"></span></div>
                                            No
</label>


<input class="styled" htmlattributes="{ checked = true }" id="Yes" name="Chapel" type="radio" value="Yes" autocomplete="off">

1 个答案:

答案 0 :(得分:1)

您需要在表单提交上执行此操作,您当前正在设置文档加载和输入单击时所需的attr。达到这样:

$('form').submit(function(e)
{
    e.preventDefault(); //stop form submitting straight away

    var radio = $('.my-radio');

    if (radio.val() === 'foo') {
        //disable require msg e.g. (this is an example - not a copy + paste)
        $('form input').each(function()
        {
            $(this).removeAttr('required')
        })
    }

    $(this).submit()
})

REF:

https://api.jquery.com/removeAttr/