javascript/html:make input fields required on checkbox check

巧了我就是萌 提交于 2021-01-28 08:30:49

问题


So like I want to make an input field required when a checkbox is checked an 'not required' when it is unchecked...I've tried the following code but it was not working ...please help...

<form action="#">
    <input id='req'><input type="submit"></form><input type="checkbox" onclick="req()" id="check">
    <script>

    function req(){
        var req = document.getElementById("req");
        var checkBox = document.getElementById("check");
        if (checkBox.checked == true){
            alert("checked")
            req.required==true

        } else {
          alert("uncheck")
          req.required==false
        }

    }



    </script>

回答1:


Give this a try:

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
    <title>Ilan's Test</title>
</head>

<body>

    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div id="results">
                    <form name="test" id="test" action="#" method="GET">
                        <div class="input-group">
                            <div class="form-check">
                                <input type="checkbox" class="form-check-input" id="cbox">
                                <label class="form-check-label" for="cbox">Make field required</label>
                            </div>
                        </div><br>

                        <div class="input-group">
                            <input type="text" id="tbox" class="form-control">
                        </div><br>
                        <button type="submit" id="sub" class="btn btn-primary">Submit</button><br>
                        <small><span class="status"></span></small>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
    <script>
        $(document).ready(function() {
            // On click of checkbox
            $('#cbox').click(function() {
                // Check if it's checked
                if ($(this).prop('checked')) {
                    // Add the required attribute to the text input
                    $('#tbox').attr('required', '');
                    // log our results and display a small indicator span status
                    console.log('input is now required');
                    $('.status').text('input is now required');
                } else {
                    // If it isn't checked, remove the required attribute
                    $('#tbox').removeAttr('required');
                    // log our results and display a small indicator span status
                    console.log('input is no longer required');
                    $('.status').text('input is no longer required');
                }
            });
        });
    </script>
</body>

</html>


来源:https://stackoverflow.com/questions/51409536/javascript-htmlmake-input-fields-required-on-checkbox-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!