HTML - 如何在加载时使用已知值预填充表单字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4055199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:01:46  来源:igfitidea点击:

HTML - How to pre-populate form field with known value upon load?

html

提问by user118190

I have this web page, say Home.html, that has links to other employee-related web sites. There is one, say www.SomeSite.com/HR.xyz, where it has a form to login. There are 3 fields: UserId, Password, and Company. The company is always the same, e.g. MyCo. How can I update the Home.html page so that when the user clicks on the link to , www.SomeSite.com/HR.xyz, it automatically populates the Company field with "MyCo"?

我有这个网页,比如 Home.html,它有指向其他员工相关网站的链接。有一个,比如 www.SomeSite.com/HR.xyz,它有一个登录表单。有 3 个字段:UserId、Password 和 Company。公司总是相同的,例如 MyCo。如何更新 Home.html 页面,以便当用户单击 www.SomeSite.com/HR.xyz 的链接时,它会自动使用“MyCo”填充公司字段?

I looked at the DOM and found the following:

我查看了 DOM,发现以下内容:

<input autocomplete='off' class='loginInput' tabindex='3' type="text" name="company" id="company" value="" maxlength='50' size="25">

<input autocomplete='off' class='loginInput' tabindex='3' type="text" name="company" id="company" value="" maxlength='50' size="25">

I tried the following by entering it in the URL and it did not work (no error, just didn't populated the "company" field: www.SomeSite.com/HR.xyz?company=MyCo.

我通过在 URL 中输入它来尝试以下操作,但没有奏效(没有错误,只是没有填充“公司”字段:www.SomeSite.com/HR.xyz?company=MyCo.

Thanks for advice!

谢谢你的建议!

回答by Seb

Change the valueattribute:

更改value属性:

<input autocomplete='off' class='loginInput' tabindex='3' type="text"
name="company" id="company" value="MyCo" maxlength='50' size="25">

回答by ahmedabdihakim

You could try using a get request to populate the input box in the form. That's only if the url like you said is as such www.SomeSite.com/HR.xyz?company=MyCo. In PHP you would simply include:

您可以尝试使用 get 请求来填充表单中的输入框。只有当您说的网址是 www.SomeSite.com/HR.xyz?company=MyCo 时。在 PHP 中,您只需包括:

<input autocomplete='off' class='loginInput' tabindex='3' type="text" name="company" id="company" value="<?php echo $_GET["company"]; ?>" maxlength='50' size="25">

<input autocomplete='off' class='loginInput' tabindex='3' type="text" name="company" id="company" value="<?php echo $_GET["company"]; ?>" maxlength='50' size="25">

As you can see that within the value attribute is a echo statement that echoes the get request in the URI where HR.xyz?company=MyCo contains the company get request. If you are using just pure html with no scripting language like php the only other method is by having this code:

正如您所看到的,在 value 属性中是一个 echo 语句,它在 URI 中回显获取请求,其中 HR.xyz? company=MyCo 包含公司获取请求。如果您只使用纯 html 而没有像 php 这样的脚本语言,那么唯一的其他方法是使用以下代码:

<input autocomplete='off' class='loginInput' tabindex='3' type="text" name="company" id="company" value="myCo" maxlength='50' size="25">

<input autocomplete='off' class='loginInput' tabindex='3' type="text" name="company" id="company" value="myCo" maxlength='50' size="25">

回答by Reese Currie

If your users universally use Internet Explorer, you could do something a bit hackish with VBScript, e.g.

如果您的用户普遍使用 Internet Explorer,您可以使用 VBScript 做一些有点骇人听闻的事情,例如

Set IE = CreateObject("InternetExplorer.Application")

Set IE = CreateObject("InternetExplorer.Application")

IE.Navigate "http://www.SomeSite.com/HR.xyz"

IE.Visible = True

IE.Navigate "http://www.SomeSite.com/HR.xyz"

IE.Visible = True

IE.Document.All.Item("company").Value = "MyCo"

IE.Document.All.Item("company").Value = "MyCo"

But that would be hard to distribute to your users because they would have to click through some security errors, and it requires IE.

但这很难分发给您的用户,因为他们必须点击一些安全错误,并且需要 IE。

If they're Firefox users I would suggest looking into Mozilla Autofill.

如果他们是 Firefox 用户,我建议查看 Mozilla Autofill。

回答by dotariel

You need some way of reading the value passed in the URL on the HR.xyz page. You need to either use Javascript or some server side logic (PHP, .NET, etc)

您需要某种方式来读取 HR.xyz 页面上 URL 中传递的值。您需要使用 Javascript 或某些服务器端逻辑(PHP、.NET 等)