单击单选按钮后,Google Recaptcha未显示

时间:2016-06-24 19:51:20

标签: javascript jquery recaptcha cakephp-2.3

我有两个单选按钮。当我点击其中一个更改表单字段时,验证码版本1不再显示。

因此,我必须单击刷新按钮以生成新的验证码图像。

<input type="radio" name="data[Form][sv]" id="FormV1" value="1" /> V1
<input type="radio" name="data[Form][sv]" id="FormV2" value="2" checked="checked" /> V2

jquery代码是:

$(document).ready(function () {
    $("#FormV1").bind("change", function (event) {
        $.ajax({async:true, 
        beforeSend:function (XMLHttpRequest) {
            $('#loading').show();
        }, 
        complete:function (XMLHttpRequest, textStatus) {$('#loading').hide()}, 
        data:$("#FormularioSituacaoVeiculo1").closest("form").serialize(), 
        dataType:"html", 
        evalScripts:true, 

success:function (data, textStatus) {
    $("#search").html(data);}, 
    type:"post", 
    url:"\/mysite\/theurl\/"});
    return false;
});

$("#FormV2").bind("change", function (event) {
    $.ajax({async:true, 
    beforeSend:function (XMLHttpRequest) {
    $('#loading').show();
}, 
    complete:function (XMLHttpRequest, textStatus) {
        $('#loading').hide()
    }, 
    data:$("#FormV2").closest("form").serialize(), 
    dataType:"html", evalScripts:true, success:function (data, textStatus) {
        $("#search").html(data);
    }, 
    type:"post", 
    url:"\/mysite\/url\/"});
    return false;
});

function showRecaptcha(element) {
    Recaptcha.create('hjfsdjklsdjklfsdjklfsdjklfjksdl', element, {
        lang : 'en-gb',
        theme : 'custom',
        custom_theme_widget: 'recaptcha_widget',
        callback: Recaptcha.focus_response_field
    }
);
}

showRecaptcha('recaptcha_div');

如何切换表单字段(V1到V2)并自动生成验证码而无需单击刷新按钮?

今天点击单选按钮时不会生成验证码。因此,我必须单击刷新按钮以重新生成验证码图像。

2 个答案:

答案 0 :(得分:11)

请查看此工作示例(我已尽力使此示例尽可能接近您的情况):

$('input[name="data[Form][sv]"]').change(function(e) {
  $.ajax({
    async: true,
    beforeSend: function(XMLHttpRequest) {
      $('#loading').show();
    },
    complete: function(XMLHttpRequest, textStatus) {
      $('#loading').hide()
    },
    data: $(this).closest("form").serialize(),
    evalScripts: true,
    success: function(data, textStatus) {
      $("#search").html(data);
      Recaptcha.reload();
    },
    dataType: "html",
    type: "POST",
    url: "https://httpbin.org/post"
  });
  return false;
});

function showRecaptcha(element) {
  Recaptcha.create('6Ld3TPkSAAAAAPckREYZuVQ6GtjMQsD-Q5CkzNxj', element, {
    lang: 'pt-BR',
    theme: 'white',
    custom_theme_widget: 'recaptcha_widget',
    //callback: Recaptcha.focus_response_field
  });
}

$(function() {
  showRecaptcha('recaptcha');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
<div id="recaptcha"></div>

<h4>Radio Inputs: </h4>
<form>
  <input type="radio" name="data[Form][sv]" id="FormV1" value="1" />V1
  <input type="radio" name="data[Form][sv]" id="FormV2" value="2" checked="checked" />V2
</form>
<h4>Ajax Post Result <span style="display:none" id="loading">Loading...</span>: </h4>
<div id="search">

</div>

答案 1 :(得分:1)

您正在寻找刷新代码吗?

代表v1:

Recaptcha.reload(); 

for v2:

grecaptcha.reset();