Javascript prop('required', true) 有效但 prop('required', false) 不行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7264938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:33:10  来源:igfitidea点击:

Javascript prop('required', true) works but prop('required', false) doesn't?

javascriptjquery

提问by MFB

Why does the $('#select_embed') property get set to true, but not to false? Instead, I tried removeAttribute('required'), but that didn't work either.

为什么 $('#select_embed') 属性被设置为 true,而不是 false?相反,我尝试了 removeAttribute('required'),但这也不起作用。

<script>

    function showBundles(){
        if (document.getElementById("embed").checked){
            $('#div_embed_bundles').show('fast')
            $('#select_embed').prop('required',true);
        }
        else {
            $('#div_embed_bundles').hide('fast')
            $('#select_embed').prop('required',false);
        }
    }

</script>

回答by ShankarSangoli

Ideally if prop('required',true)is working then prop('required',false)should also work. But you can try with removeAttr, hope this helps.

理想情况下,如果prop('required',true)正在工作,那么prop('required',false)也应该工作。但是您可以尝试使用removeAttr,希望这会有所帮助。

function showBundles(){
        if (document.getElementById("embed").checked){
            $('#div_embed_bundles').show('fast')
            $('#select_embed').prop('required',true);
        }
        else {
            $('#div_embed_bundles').hide('fast')
            $('#select_embed').removeAttr('required');
        }
    }