You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Grails中使用g:actionSubmit打开新窗口无法指定尺寸该如何解决?

问题原因

你原有代码中g:actionSubmit的params是Grails标签用于向后端接口传递请求参数的属性,完全不会作用于浏览器的弹窗行为。formtarget="_blank"仅能指定表单提交后在新标签/新窗口打开,默认遵循浏览器默认的新窗口打开规则,无法自定义尺寸。

实现方案

使用JavaScript的window.open方法控制弹窗属性,有两种常用实现方式:

方案1:绑定按钮点击事件主动触发弹窗

<!-- 给表单设置唯一ID -->
<g:form controller="myController" action="index" id="myForm">
  <button type="button" onclick="openCustomWindow()">View the New Page</button>
</g:form>

<script>
function openCustomWindow() {
  const form = document.getElementById('myForm');
  // 构造表单提交地址,Get请求示例
  const submitUrl = form.action + '?' + new URLSearchParams(new FormData(form)).toString();
  // 自定义弹窗参数
  window.open(
    submitUrl,
    'customWindow',
    'width=200,height=500,top=100,left=100,toolbar=no,menubar=no,scrollbars=no,resizable=no'
  );
}
</script>

方案2:拦截表单提交事件控制弹窗

如果需要保留g:actionSubmit的原有逻辑,可以拦截表单提交行为:

<g:form controller="myController" onsubmit="openCustomWindow(this); return false;">      
  <g:actionSubmit value="View the New Page" action="index"/> 
</g:form>

<script>
function openCustomWindow(form) {
  // 先创建指定尺寸的空窗口
  window.open('', 'customWindow', 'width=200,height=500,top=100,left=100');
  // 指定表单提交到刚才创建的自定义窗口
  form.target = 'customWindow';
  form.submit();
}
</script>
注意事项
  • 主流浏览器都会拦截非用户主动触发的window.open弹窗,所以必须将弹窗触发逻辑绑定在用户点击、提交等直接交互事件中,不要在异步回调内调用,避免被拦截。
  • window.open第三个参数支持多个窗口属性配置,可按需调整:width/height控制窗口宽高,top/left控制窗口在屏幕的初始位置,toolbar=no隐藏浏览器工具栏,resizable=no禁止用户调整窗口大小。

内容的提问来源于stack exchange,提问作者Simon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 19:36:03