如何从 url 参数预填充 html 表单输入字段? [英] How can I pre-populate html form input fields from url parameters?

查看:43
本文介绍了如何从 url 参数预填充 html 表单输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有表单的普通 html 页面.需要能够通过 url 预填充表单.类似的东西:

I have a vanilla html page which has a form in it. A requirement has come in to be able to pre-populate the form via the url. Something like:

http://some.site.com/somePage.html?forename=Bob&surname=Jones

我似乎找不到任何简单的解决方案.有人可以用一些 javascript 指出我正确的方向来完成这个吗?很高兴使用 javascript 解决方案,但我更愿意避免为这种一次性使用而引入整个库(目前没有使用).谢谢.

I can't seem to find any simple solution to this. Can someone point me in the right direction with some javascript to accomplish this? Happy to use a javascript solution, but I'd prefer to avoid pulling in an entire library just for this single use (none are currently used). Thanks.

推荐答案

使用自定义查询字符串 Javascript 函数.

Use a custom query string Javascript function.

function querySt(ji) {

    hu = window.location.search.substring(1);
    gy = hu.split("&");

    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}
var koko = querySt("koko");

然后将检索到的值赋给输入控件;类似:

Then assign the retrieved value to the input control; something like:

document.getElementById('mytxt').value = koko;

这篇关于如何从 url 参数预填充 html 表单输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆