[英]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!"/>
<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.