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

如何将搜索URL中的+替换为-?跨站搜索适配求助

解决表单提交时空格转-而非+的问题

浏览器默认的GET表单提交会自动将空格编码为+(或%20),这和目标网站要求的-分隔符不匹配。我们可以通过JavaScript自定义表单提交逻辑,手动处理搜索关键词并构造符合要求的URL。

具体解决方案

只需要给表单添加提交事件监听,阻止默认提交行为,然后将输入框中的空格替换为-,最后跳转到目标地址即可。以下是完整的代码示例:

<form class="example" onsubmit="handleSearch(event)" style="margin:auto;max-width:300px">
  <input type="text" placeholder="Movie Name Here..." name="search" id="searchInput">
  <button type="submit"><i class="fa fa-play"></i></button>
</form>

<script>
function handleSearch(event) {
  // 阻止表单默认的GET提交行为,避免浏览器自动编码空格
  event.preventDefault();
  
  // 获取输入框的搜索关键词并去除首尾空格
  const searchQuery = document.getElementById('searchInput').value.trim();
  
  // 处理空输入的情况(可选,根据需求调整)
  if (!searchQuery) {
    alert('请输入要搜索的影片名称');
    return;
  }
  
  // 将所有空格(包括连续多个空格)替换为`-`
  const formattedQuery = searchQuery.replace(/\s+/g, '-');
  
  // 构造符合目标网站要求的URL并跳转
  const targetUrl = `https://europixhd.io/svop2/zznewsrv4?search=${formattedQuery}`;
  window.location.href = targetUrl;
}
</script>

代码关键点说明

  • event.preventDefault():阻止表单的默认提交流程,避免浏览器自动对关键词进行URL编码。
  • replace(/\s+/g, '-'):使用正则表达式匹配所有空格(包括连续多个空格),统一替换为-,确保和目标网站的格式一致。
  • 手动构造URL并跳转:通过window.location.href直接跳转到处理后的目标地址,完全按照我们需要的格式传递搜索参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 10:42:47