javascript - 将变量值放入文本框中,而不使用 JavaScript 中的 onclick 按钮

标签 javascript html

我已经编写了带有以下注释的代码:

它正在存储来自 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/

相关文章:

javascript - 连接到 mongodb 时连接不是函数

html - 不能将文本对齐和宽度设置为 html 样式

css - Div float 在其他 Div 中

javascript - 自动缩放内容、标题和其他字体大小

JavaScript 碰撞检测错误

javascript - 在回调函数中保留 "this"

javascript - 如何在 HTML 中使用上下文菜单

javascript - 如何在javascript中获取字符串中的撇号

javascript - 单选按钮 : "Uncaught syntax error, unrecognized expression" 的 jQuery 值

javascript - 按键事件被触发两次