填写输入文字

Jea*_*ean 4 javascript

如何用值填充文本输入?我在尝试这个:

<input id="curso" maxlength="150" name="curso" size="40" type="text" 
    value="window.location.href.substring(91))" />
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

Mik*_*uel 9

<input id="curso" maxlength="150" name="curso" size="40" type="text">

<script>
document.getElementById("curso").value =
document.getElementById("curso").defaultValue = window.location.href.substring(91);
</script>
Run Code Online (Sandbox Code Playgroud)

您无法从任意DOM属性中执行JavaScript.只有<script>元素和事件处理程序可以包含JavaScript,因此添加一个<script>设置值的.

您还需要设置该defaultValue属性,以便任何表单重置将值重置为所需的值.