开发者

fill input text

开发者 https://www.devze.com 2023-04-07 00:26 出处:网络
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\"

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

精彩评论

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