具有价值的 InnerHTML

InnerHTML with value

我需要有关以下代码的帮助。我需要用户将他们的内容输入到占位符中,而不是在脚本中,这样当用户单击按钮时,结果就会显示出来。

在这个例子中,"my test.asp?name=ståle&car=saab"是示例内容,但我需要将它们放在占位符中,而不是在脚本中。

我用值更改了 InnerHTML,但它似乎不起作用。谁能帮忙?非常感谢!

<body>

<p>Enter foregin character and click covert</p>

<input id="demo" placeholder="Keyword">

<button onclick="myFunction()">Convert</button>

<p id="demo"></p>

<script>
function myFunction() {
var uri = "my test.asp?name=ståle&car=saab";
var res = encodeURI(uri);
document.getElementById("demo").innerHTML = res;
}
</script>

You can set any value in the placeholder using script.

document.getElementById("xyz").placeholder = "Any value";

Check this out at: http://www.w3schools.com/jsref/prop_text_placeholder.asp

<p>Enter foreign character and click convert</p>

<input id="demo1" placeholder="Keyword"> <!-- different id from <p> tag -->

<button onclick="myFunction()">Convert</button>

<p id="demo2"></p> <!-- different id from <input> tag -->

<script>
function myFunction() {
    var uri = document.getElementById("demo1").value; // This was missing, get the value the user has typed
    var res = encodeURI(uri);
    document.getElementById("demo2").innerHTML = res;
}
</script>

看到这个JSFiddle