javascript 如何使输入提交元素在我的输入复选框元素被选中后出现

cdmah0mi  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(89)

我正在为学校的一个项目编写一个假的reCAPTCHA框。我已经完成了代码,但我想让它,所以你可以先提交你的答案,从输入元素后,复选框的reCAPTCHA元素。从我的研究中,我可以告诉我需要JavaScript,但我从来没有学会过编码,所以我迫切需要帮助。

输入元素

<form action="/action_page.php">
    <label for="fname">First name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last name:</label>
    <input type="text" id="lname" name="lname"><br><br>
</form>

<section>
    <label for="check">
        <div class="input-container">
          <input type="checkbox" class="checkbox" id="check">
          <span class="checkbox-text">I'm not a robot</span>
        </div>
      </label>

    <img src="captcha.png" alt="" class="capcap">
    <span class="recaptcha">reCAPTCHA</span>
    <br>
    <a href="#" class="hi">Privacy</a>
    <span class="dash">-</span>
    <a href="#" class="hi">Terms</a>
</section>

<p>Please check of the box to verify you're not a robot.</p>

<script>
    if (checkbox is checked) {
        input type="submit" value="Submit"
    }
</script>

我试着编写JavaScript,但老实说,我看的toturial并没有太大帮助,让我非常困惑。

n53p2ov0

n53p2ov01#

试试这个:

document.getElementById("check").addEventListener("change", function () {
        var submitBtn = document.getElementById("submitBtn");

        // If the checkbox is checked, enable the "Submit" button; otherwise, disable it
        if (this.checked) {
            submitBtn.removeAttribute("disabled");
        } else {
            submitBtn.setAttribute("disabled", "true");
        }
    });
<form action="/action_page.php">
    <label for="fname">First name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last name:</label>
    <input type="text" id="lname" name="lname"><br><br>

    <label for="check">
        <div class="input-container">
            <input type="checkbox" class="checkbox" id="check">
            <span class="checkbox-text">I'm not a robot</span>
        </div>
    </label>

    <img src="captcha.png" alt="" class="capcap">
    <span class="recaptcha">reCAPTCHA</span>
    <br>
    <a href="#" class="hi">Privacy</a>
    <span class="dash">-</span>
    <a href="#" class="hi">Terms</a>
</form>
<p>Please check the box to verify you're not a robot.</p>
<button type="submit" id="submitBtn" disabled>Submit</button>

相关问题