首页 > 解决方案 > 需要启用按钮 WHEN Recaptcha 和复选框已被验证

问题描述

我对 Django 和 jQuery 很陌生,所以在这里需要一些帮助(基本上是菜鸟)。从服务器端验证验证码后,我需要启用开始测试按钮,不能从前端绕过并且复选框已被选中我之前做过复选框,但不知道如何添加验证码。

代码

<div
  class="g-recaptcha captcha-button"
  align="center"
  data-sitekey="sitekey"
></div>
<br />
<div class="form-check test-check-button" align="center">
  <input type="checkbox" class="form-check-input display-7" id="instructions" />
  <label class="form-check-label text-center" for="instructions">
    <strong>I have read the instructions given above.</strong></label
  >
</div>
<div class="mb-5 mt-2 text-center start-button" id="startButton"></div>

<script>
  $("#instructions").on("click", function () {
    $.ajax({
      type: "GET",
      url: "/test/confirm",
      data: {
        checkbox: 1,
      },
      dataType: "json",

      success: function (data) {
        if (data.button) {
          $(".test-check-button").html("");
          $(".start-button").html(data.button);
        }
      },
    });
  });
</script>

视图.py

def confirm(cls, request: HttpRequest):
        button = '<button class="btn btn-primary mt-3" id="submit_test" type="submit">Start 
           Test</button>'
        data = {'button': button}
        return JsonResponse(data)

项目中没有forms.py。所以想知道如何在没有表单的情况下验证验证码服务器端并在验证和复选框被选中后启用“开始测试”按钮。谢谢你。

没有开始按钮

带开始按钮

标签: jquerydjango

解决方案


有一个数据回调属性:

function myCallback(){
  $('button').prop( "disabled", false )
}
<div class="g-recaptcha" data-callback="myCallback"/>


推荐阅读