gpt4 book ai didi

javascript - 拆分链接并插入到表单中

转载 作者:行者123 更新时间:2023-11-30 15:23:29 24 4
gpt4 key购买 nike

在我的网站上,我有一个如下所示的 HTML 表单:

<label>Name</lable>
<input name="username" id="username19" type="text" value="">

假设浏览器地址栏中的链接是:

https://sitename.com/consulting/order-plan/#ref/testing**

我想做以下事情:

从浏览器地址栏中选择 URL 并以“/”分隔。在本例中选择最后一个词后,它是“testing”并将其插入到 ID 为“username19”的表单值中

你能帮我用 Javascript 或 jQuery 来做吗?

最佳答案

使用 document.URL 获取当前 url 并使用正则表达式获取最后一个词

var url = document.URL;
var match = url.match(/([^/]*)$/);

console.log(url);
console.log(match);

document.getElementById("username19").value = match[1];
<label for="username19" >Name</lable>
<input name="username" id="username19" type="text" value="">

关于javascript - 拆分链接并插入到表单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43343643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com