Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript GoogleSignup并没有在第一次点击元素上运行,但在那个之后运行良好。为什么?_Javascript_Jquery_Google Signin - Fatal编程技术网

Javascript GoogleSignup并没有在第一次点击元素上运行,但在那个之后运行良好。为什么?

Javascript GoogleSignup并没有在第一次点击元素上运行,但在那个之后运行良好。为什么?,javascript,jquery,google-signin,Javascript,Jquery,Google Signin,您好,我正在使用谷歌api登录到我的web应用程序。工作正常。但问题是sigin在我的应用程序中是自动的,但对于注册,我希望在用户检查条款和条件后启动该过程,该过程将启动 下面是JS代码: var googleUser = {}; var auth2; var startApp = function() { gapi.load('auth2', function(){ auth2 = gapi.auth2.init({ client_id

您好,我正在使用谷歌api登录到我的web应用程序。工作正常。但问题是sigin在我的应用程序中是自动的,但对于注册,我希望在用户检查条款和条件后启动该过程,该过程将启动

下面是JS代码:

var googleUser = {};
var auth2;
var startApp = function() {
    gapi.load('auth2', function(){
    
        auth2 = gapi.auth2.init({
            client_id: 'XXXXXX',
                
        });
        $("body").on("click", ".googlesignin", function(e){
            
            if($('#terms').is(":checked")){
                termsvalue='1';
                $("#termsValidation").hide();
                $("#termsValidation").attr("data-hint", $("").text()); 
                attachSignup(document.getElementById("googlesignin"));
            }
            else
            {
                e.preventDefault();
                termsvalue='';
                $("#termsValidation").show();
                $("#termsValidation").attr("data-hint", "Please agree by clicking checkbox");
                return false;
            }
        });
        
    });
};



function attachSignup(element) {  

    auth2.attachClickHandler(element, {},
        function(googleUser) { 
            var profile = googleUser.getBasicProfile();
            console.log(googleUser.getAuthResponse().id_token);
            var googleurl = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" + googleUser.getAuthResponse().id_token
             $.getJSON(googleurl, function(result){
                $("#rfirstname").val(result.given_name);
                $("#remail").val(result.email);
                $("#googlePicture").val(result.picture);
                $("#google_id").val(result.sub);
                    
            });
            
        }, function(error) {
        parsed_error = JSON.stringify(error, undefined, 2);
        parsed_error = JSON.parse(parsed_error); 
          if(popup_closed_by_user!='popup_closed_by_user'){
            alert(popup_closed_by_user);
          }   
        });
  };

  startApp();
这是我的html:

<html>
<head>
<meta name="google-signin-client_id" content="XXXXXX">
<script src="https://apis.google.com/js/api:client.js"></script>
</head>
<body>
<div>
    <input type="checkbox" id="terms">
    <div id="googlesignin" class="googlesignin"><img alt="" src="google.png" style="vertical-align: initial;"></div>
</div>
</body>
</html>


在第一次单击中,它不工作,但在那之后,它工作正常。我需要帮助。提前感谢。

我建议尝试注释
startApp
的最后一行,并在下面添加jQuery加载时的挂钩。。像这样,希望这会有帮助

  // startApp();
  
  (function($){
    $(document).ready(function(){
      startApp();
    }); // - end DOCREADY
  })(jQuery);

hej@Jeevan,您还需要给我们提供html,以便能够构建和复制您的问题-请参阅此处的详细信息>您正在附加
click
event@body child
$(“body”)。在(“click”,“.googlesign”..
内部
gapi.load
?这就是你的窍门。@KresimirPendic这是我的html@KresimirPendic如果我在gapi加载之外编写click事件,那么什么都不会发生。你有更好的解决方案吗?@KresimirPendic如果我调用我的attachSignup()还有一件事功能超出我的单击事件,然后它工作正常,但此时我无法检查复选框条件。是的,我已尝试此操作,但没有影响。再次单击时它工作正常。