Javascript 当radiobutton被选择为No时,禁用require消息

Javascript 当radiobutton被选择为No时,禁用require消息,javascript,jquery,Javascript,Jquery,当我选中单选按钮“否”时,我试图禁用所需消息 然而,当我提交表单时,什么也并没有发生,它只是重定向到同一个页面,并显示所需的消息 更新代码 //hide/show chapela Yes/No $(document).ready(function () { $("input[name$='Chapel']").click(function () { var test = $(this).val(); if (test ==

当我选中单选按钮“否”时,我试图禁用所需消息 然而,当我提交表单时,什么也并没有发生,它只是重定向到同一个页面,并显示所需的消息

更新代码

//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);
        } 
    });

@LabelFor(model=>model.Chapel,htmlAttributes:new{@class=“controllabel col-md-3”})
@RadioButton(model=>model.Chapel,“Yes”,new{id=“Yes”,@class=“styled”,htmlAttributes=new{@checked=“true”})
对
@RadioButton(model=>model.Chapel,“No”,new{id=“No”,@class=“styled”,htmlAttributes=new{@checked=“true”})
不
@Html.ValidationMessageFor(model=>model.Chapel,“,new{@class=“text danger”})
呈现的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">

您需要在表单提交时执行此操作,您当前正在为文档加载和输入单击设置所需的属性。做到这一点:

$('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()
})
参考:


请显示呈现的html而不是当前的:)呈现的html。在页面上单击鼠标右键,然后单击“检查”(
Ctrl+Shift+I
)并复制html,但不是全部,而是仅复制此处显示的此片段。@KrzysztofJaniszewski检查呈现html部分。我为单选按钮添加了代码。如果您需要更多代码,请告诉我好的,我知道了,什么元素有
id=“hideChapel”
?这一行混淆了
$(“div#hideChapel”).prop('required',false)
div
不能有属性
必需
那么,你知道什么?如何重新更改这部分代码?谢谢@ThisGuyHasTwoThumbs:)@Joe123没问题^。^//禁用require msg例如(这是一个示例-不是复制+粘贴)这部分代码是什么意思D@Joe123这是示例代码,如果您直接复制并粘贴到代码中,它将不起作用(如果您以这种方式设置表单,可能会起作用,但它更像是一个演示:))是的,我明白了。但我知道我在填写表格时遇到了问题,提交按钮不起作用