onchange提交表格带有页面锚

发布于 2025-02-12 11:49:56 字数 253 浏览 1 评论 0原文

无论如何,在提交时,使用类似此代码的东西可以通过HTML锚来跳到页面中的特定位置?

onChange="document.form2.submit()"

我在选择后提交的下拉菜单上面使用此代码,但是我还需要它传递页面锚。.这可行吗?

非常感谢

PS锚将取决于使用了哪个下拉菜单。例如,页面中有大约40个下拉菜单。因此,当您超过20个时,您必须继续向下滚动以选择下一个。下拉次数...希望这是有道理的。

Using something like this code is there anyway of passing a html anchor to jump to a particular place in the page when it has been submitted?

onChange="document.form2.submit()"

Im using this code above for a dropdown menu which submits after selection, but i also need it to pass a page anchor.. is this doable?

many thanks

ps The anchor will depend on which dropdown menu was used. There are, for example, around 40 dropdown menus in a page.. so when you get over 20 you have to keep scrolling down to select the next one.. this is why i need the page, after submitting, to jump to the last dropdown used... Hope this makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2025-02-19 11:49:57

提交之前,将表单的操作设置为url#您的锤子

function change_url(form, hash) {
  form.setAttribute("action", "https://example.com/#" + hash);
  form.submit();
}

document.querySelectorAll("select").forEach(function(elem) {
  elem.addEventListener("change", function() {
    var hash = this.getAttribute("name");
    var form = this.closest("form")
    change_url(form, hash);
  })
})
<h1>fill the form</h1>
<form action="" method="get" onsubmit="change_url(this); return false">


  <a name="sel1"></a>
  <select name="sel1">
    <option></option>
    <option>sel1</option>
  </select>

  <div style="height:500px"></div>

  <a name="sel2"></a>
  <select name="sel2">
    <option></option>
    <option>sel2</option>
  </select>

  <div style="height:500px"></div>

  <a name="sel3"></a>
  <select name="sel3">
    <option></option>
    <option>sel3</option>
  </select>

  <div style="height:500px"></div>

  <button>submit</button>
</form>

Set the action of the form to url#your-hash right before you submit.

function change_url(form, hash) {
  form.setAttribute("action", "https://example.com/#" + hash);
  form.submit();
}

document.querySelectorAll("select").forEach(function(elem) {
  elem.addEventListener("change", function() {
    var hash = this.getAttribute("name");
    var form = this.closest("form")
    change_url(form, hash);
  })
})
<h1>fill the form</h1>
<form action="" method="get" onsubmit="change_url(this); return false">


  <a name="sel1"></a>
  <select name="sel1">
    <option></option>
    <option>sel1</option>
  </select>

  <div style="height:500px"></div>

  <a name="sel2"></a>
  <select name="sel2">
    <option></option>
    <option>sel2</option>
  </select>

  <div style="height:500px"></div>

  <a name="sel3"></a>
  <select name="sel3">
    <option></option>
    <option>sel3</option>
  </select>

  <div style="height:500px"></div>

  <button>submit</button>
</form>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文