我已经编写了带有以下注释的代码:
它正在存储来自 url=> ../webdev/dummy.html?foo=quora 的值,现在我的查询是我想在文本框中显示 quora仅页面加载,我不想使用 onclick 功能。请指导我:)
dummy.html
<script type="text/javascript">
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var foo = getParameterByName('foo');
document.getElementsByName('subject').Value = foo; // this is I am thinking of to store value in textbox id="subject"
</script>
<input type="text" name="subject" id="subject" readonly="true" />
<!-- textbox to display "quora" -->
文本框中没有显示任何值
最佳答案
您必须替换代码
document.getElementsByName('subject').Value = foo;
与
document.getElementById('subject').value = foo;
如果您想在页面加载时调用它,可以使用以下代码,
body.onload=function(){//调用你的函数};
或
window.onload=function(){//调用函数}
关于javascript - 将变量值放入文本框中,而不使用 JavaScript 中的 onclick 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40779251/