H开发者_StackOverflow社区ow can I fill a text input with a value? I am trying this:
<input id="curso" maxlength="150" name="curso" size="40" type="text"
value="window.location.href.substring(91))" />
But that does not work.
<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>
You can't execute JavaScript from within an arbitrary HTML attribute. Only <script>
elements and event handlers can contain JavaScript, so add a <script>
that sets the value.
You also need to set the defaultValue
property so that any form reset resets the value to the desired value.
精彩评论