简体   繁体   English

g:submitButton- onclick不起作用

[英]g:submitButton- onclick doesn't work

I have the following g:submitButton : 我有以下g:submitButton

 <g:submitButton name="next" class="signup-button skyblue-btn pull-right pl-pr-36" value="${message(code:"buttons.ok")}" onclick="return showSpinnerSignUp()"/>

I define the showSpinnerSignUp() in the JS file: 我在JS文件中定义了showSpinnerSignUp()

$(function() {
    function showSpinnerSignUp(){
        $('.spinner-ctn').show();
        return true;
    }
});

The spinner is not displayed (the onclick doesn't work). 微调器不会显示( onclick无效)。

This is the default behaviour of a form submission in the browser. 这是浏览器中表单提交的默认行为。 You have registered a showSpinnerSignUp() method on the click of a button while the click on that same button is responsible for submitting the enclosing form. 单击按钮时,您已经注册了showSpinnerSignUp()方法,而单击同一按钮则负责提交showSpinnerSignUp()

Since the browser's built-in behavior for submitting forms works by making a full roundtrip to the server, that immediately interrupts your code execution of onclick event because your browser is navigating away from your current page. 由于浏览器的内置表单提交行为是通过与服务器的完整往返来实现的,因此,由于浏览器正在离开当前页面,因此这立即中断了onclick事件的代码执行。

This can be simulated as if you had clicked your button and refreshed the page immediately. 可以像单击按钮并立即刷新页面一样模拟。 Your current setup might work when you deploy this to the production server, but the possibility of working this setup locally is low because a local development server used to run on a faster machine so the page refresh time of form submission is quite faster. 当您将当前的设置部署到生产服务器时,当前的设置可能会起作用,但是在本地使用此设置的可能性很低,因为过去本地开发服务器运行在更快的计算机上,因此表单提交的页面刷新时间相当快。

To verify this happening, open your browser's console and call the method showSpinnerSignUp() directly (remember to remove it from $(function() {}) temporarily) and see if the spinner is showing up. 要验证这种情况,请打开浏览器的控制台并直接调用方法showSpinnerSignUp() (请记住暂时将其从$(function() {})删除$(function() {}) ),然后查看微调器是否正在显示。

Also, there is an option to Preserve Logs which keeps the Javascript console logs even after page refresh. 此外,还有一个“ Preserve Logs选项,即使在刷新页面后,该选项仍Preserve Logs Javascript控制台日志。 So, put a simple console.log() inside your method call and then try hitting that button. 因此,将一个简单的console.log()放入您的方法调用中,然后尝试单击该按钮。 You'll see your method is called but no spinner is displayed due to form submission. 您会看到您的方法被调用,但是由于表单提交而未显示任何微调器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM