根据第一个字段使用 Javascript 填充表单字段

发布于 2025-01-03 19:49:35 字数 1323 浏览 0 评论 0原文

我有一个小问题,我正在尝试寻找最佳解决方案。我有一个表单,用户将在其中输入他们复制的网址,它的格式始终类似。
像这样:http://www3.google.com/register.php?key=12334123< /a>

我希望能够根据上述输入填充两个字段。

字段 1 将是服务器(子域)

字段 2 将是键号。

我目前已经制作了一些有效的东西,但是我认为使用正则表达式来完成它比我正在做的方式更好。

对java不太热衷,这对我来说是一个挑战,任何帮助将不胜感激!

<script language="javascript">
function urlbreaker(url)
{
    var startserver = document.form1.url.value.search(/www/)

    document.form1.server.value = document.form1.url.value.substr(startserver, 4)

    var startkey = document.form1.url.value.search(/=/)
    document.form1.key.value = document.form1.url.value.substr(startkey+1, 10)
}


</script>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label for="url">input url:</label>
    <input type="text" name="url" id="url" onblur="urlbreaker()" />
   http://www3.google.com/register.php?key=12334123</p>
  <p>
    <label for="server">Server:</label>
    <input type="text" name="server" id="server" />
  should be www3</p>
  <p>
    <label for="key">key</label>
    <input type="text" name="key" id="key" />
  should be 12334123</p>

</form>

I have a slight issue I'm trying to find the best solution for. I have a form where users will enter a url that they copy, it will always be formatted similarly.
Like this: http://www3.google.com/register.php?key=12334123

I would like to be able to populate two fields based on the above input.

Field 1 will be the server (subdomain)

Field 2 will be the key number.

I currently have made something that works, however I think it would be better to do it with regex as opposed to the way I'm doing it.

Being not too keen on java this is a bit of a challenge for me, any help would be greatly appreciated!

<script language="javascript">
function urlbreaker(url)
{
    var startserver = document.form1.url.value.search(/www/)

    document.form1.server.value = document.form1.url.value.substr(startserver, 4)

    var startkey = document.form1.url.value.search(/=/)
    document.form1.key.value = document.form1.url.value.substr(startkey+1, 10)
}


</script>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label for="url">input url:</label>
    <input type="text" name="url" id="url" onblur="urlbreaker()" />
   http://www3.google.com/register.php?key=12334123</p>
  <p>
    <label for="server">Server:</label>
    <input type="text" name="server" id="server" />
  should be www3</p>
  <p>
    <label for="key">key</label>
    <input type="text" name="key" id="key" />
  should be 12334123</p>

</form>

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

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

发布评论

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

评论(2

薄荷港 2025-01-10 19:49:35
function urlbreaker(url)
{
    var match = document.form1.url.value.match(/http:\/\/(.{3,4})\..*?\..*\?(key|.*&key)=(\d*).*/);

    document.form1.server.value = match[1];
    document.form1.key.value = match[3];
}
function urlbreaker(url)
{
    var match = document.form1.url.value.match(/http:\/\/(.{3,4})\..*?\..*\?(key|.*&key)=(\d*).*/);

    document.form1.server.value = match[1];
    document.form1.key.value = match[3];
}
π浅易 2025-01-10 19:49:35

与 url 的主机部分和关键参数相匹配的简单正则表达式是:

var url = 'http://www3.google.com/register.php?key=12334123';
var match = /[^:]*\/\/([^\/]+)\/[^?]+\?[\s\S]*?key=(\d*)/m.exec(url);
var domain = match[1]; // www3.google.com
var key = match[2]; // 12334123

您正在询问服务器/子域,因此如果您想要“www3”

var url = 'http://www3.google.com/register.php?key=12334123';
var match = /[^:]*\/\/([^.]+)[^\/]*\/[^?]+\?[\s\S]*?key=(\d*)/m.exec(url);
var domain = match[1]; // www3
var key = match[2]; // 12334123

A simple regex that matches the host part and key parameter of the url would be:

var url = 'http://www3.google.com/register.php?key=12334123';
var match = /[^:]*\/\/([^\/]+)\/[^?]+\?[\s\S]*?key=(\d*)/m.exec(url);
var domain = match[1]; // www3.google.com
var key = match[2]; // 12334123

You are asking for the server/subdomain, so if you are wanting the 'www3'

var url = 'http://www3.google.com/register.php?key=12334123';
var match = /[^:]*\/\/([^.]+)[^\/]*\/[^?]+\?[\s\S]*?key=(\d*)/m.exec(url);
var domain = match[1]; // www3
var key = match[2]; // 12334123
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文