开发者

fill input text

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜