在javascript中没有onclick按钮的情况下将变量值放在文本框中

时间:2016-11-24 06:18:04

标签: 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" -->

它没有在文本框中显示任何值

4 个答案:

答案 0 :(得分:1)

您必须替换代码

document.getElementsByName('subject').Value = foo;

document.getElementById('subject').value = foo;

如果您想在页面上调用它,可以使用以下代码,

body.onload=function(){//call your function};

window.onload=function(){//call the function}

答案 1 :(得分:1)

如前所述,.getElementsByName会返回NodeList。您将不得不循环使用特定索引。

此外,如果您只有1个元素,我建议使用document.querySelector,因为这将返回第一个匹配的元素。

示例

&#13;
&#13;
var nodeList = document.getElementsByName('subject');
console.log(nodeList)
console.log(nodeList[0])

var el = document.querySelector('input[name="subject"]');
console.log(el)
&#13;
<input type="text" name="subject" id="subject" readonly="true" />
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我创建了一个工作示例:JSFiddle

&#13;
&#13;
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', 'www.google.de?foo=hugo');

document.getElementsByName('subject')[0].value = foo; // this is I am thinking of to store value in textbox id="subject"
&#13;
<input type="text" name="subject" id="subject" readonly="true" />
<!-- textbox to display "quora" -->
&#13;
&#13;
&#13;

答案 3 :(得分:1)

使用 getElementById 代替getElementsByName,并在加载DOM组件后始终运行/包含 JavaScript代码

理想情况下,HTML应始终在任何脚本之前加载样式/ DOM。

有关详细信息,请参阅:Where is the best place to put tags in HTML markup?

<input type="text" name="subject" id="subject" readonly="true" />

<!-- ideally script should run after html code -->
<script type="text/javascript">
  function getParameterByName(name, url) {
    if (!url) {
      url = window.location.href;
    }
    url = "www.example.com/webdev/dummy.html?foo=quora" //sample snippet url
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
      results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    console.log(results[2])
    return decodeURIComponent(results[2].replace(/\+/g, " "));
  }

  var foo = getParameterByName('foo');
  // changing the getElementsByName to getElementById
  document.getElementById('subject').value = foo; 
</script>