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

Google Drive高级搜索框在Google Sites回车触发额外空白标签问题求助

问题原因与修复方案

核心问题

按下回车弹出空白标签的原因是表单默认提交行为未被阻止:在表单内的输入框按回车时,浏览器会自动提交表单,而你的<form>标签未指定action属性,就会跳转到当前嵌入页面的URL(即atari-embeds.googleusercontent.com);同时你的runSearch()也会执行,因此出现两个标签页。另外代码里还有几处语法错误,比如onkeypress函数的定义完全无效。

修复步骤

  1. 给<form>添加onsubmit事件,阻止默认提交行为
  2. 移除输入框上无效的onkeypress属性,统一通过表单的onsubmit处理回车和按钮点击
  3. 删除语法错误的onkeypress函数代码

修正后的完整代码

<div class='advSearch'>
<link href='https://fonts.googleapis.com/css?family=Nunito' rel='stylesheet'>
<style>
body {
font-family: 'Nunito';font-size: 18px;
}
</style>
<!-- 添加onsubmit事件阻止默认提交,同时调用搜索函数 -->
<form onsubmit="event.preventDefault(); runSearch();">
 <input type="text" id='search' />
 <button type="submit" style='padding:5px;font-size:10px'>&#128269</button>
</form>
<script>
function runSearch() {
let q = document.getElementById("search").value;
// 可选优化:输入不为空时再打开搜索页面
if(q.trim()){
  window.open('https://script.google.com/a/macros/LINKHIDDENFORPRIVACYPURPOSES/dev/exec?q='+q);
}
}
</script>
</div>

关键改动说明

  • <form onsubmit="event.preventDefault(); runSearch();">:既阻止了表单默认跳转行为,又统一触发搜索函数,不管是按回车还是点击按钮,都只会打开搜索结果页
  • 按钮改为type="submit":让按钮和回车的行为统一,都触发表单的onsubmit事件
  • 移除了原输入框无效的onkeypress属性和错误的onkeypress函数,简化逻辑
  • 增加输入内容非空判断:避免无意义的空搜索请求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 07:56:17