繁体   English   中英

如何从文本字段中获取文本并将其作为参数传递给javascript函数

[英]How to get text from textfield and pass it as a parameter to a javascript function

我有一个文本字段,当按下提交按钮时,该字段应该更改页面上的元素以及其中的文本。 我正在将值传递给javascript函数。 它不起作用,谁能告诉我该怎么做/我在做什么错?

这是我的代码:

<html>
<head>
<script type = "text/javascript">
function submitText(string, id)
{
document.getElementById(id).innerHTML = string;
}
</script>
</head>
<body>
<p id = "Datepara" style = "font-family:comic sans ms;color:orange;text-align:center;font-size:25px;">
Date Created: 24/9/12 <br /><br /><br/>
</p>
<input id = "changetext" type = "text" value = "Enter text to display text in this webpage!"/>
&nbsp;
<input id = "submit" type = "button" onclick = "submitText('Datepara','document.getElementById("changetext").value;'" value = "Submit" />
</body>
</html>

这就是我在代码上所做的更改:我在commitText函数上切换了参数的顺序,删除了document.getElementById("changetext").value周围的引号,因为您传递的是字符串而不是输入控件的值

function submitText(id, string)
{
     document.getElementById(id).innerHTML = string;
}

<input id="submit" type="button" 
  onclick="submitText('Datepara', document.getElementById('changetext').value)"
  value="Submit" />

做这个

<input id = "submit" type = "button" onclick = "submitText('Datepara','changetext')" value = "Submit" />

javascript

function submitText(divid,txtid)
{
   document.getElementById(divid).innerHTML = document.getElementById(txtid).value;
}

'document.getElementById("changetext").value;' 是字符串文字,而不是gEBId调用的value属性。

"submitText('Datepara','document.getElementById("onclick属性的值。请注意引号。

submitText(string, id)接受一个字符串,然后是一个id,而不是id,然后是字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM