开发者

simple document failing to work

开发者 https://www.devze.com 2023-01-14 12:41 出处:网络
I\'m working on a simple script to pass the value \" ...php?answer=1\" if java is enabled. I\'ve got this far ...

I'm working on a simple script to pass the value " ...php?answer=1" if java is enabled. I've got this far ...

<sc开发者_StackOverflowript language="text/javascript">
document.form.answer.value=1;
</script>
</head>
<body>
<form name="form" action="enabled_catch.php" method="get">
<input type="hidden" name="answer">
<input type="submit" value="click me">
</form>

... but the script does not appear to ba assigning answer.value="1" - I'm not sure why. Can you help


This happens because at the moment you are assigning this value using javascript (do not confuse with Java) the DOM is not loaded yet and the form doesn't exist. Try this instead:

<script type="text/javascript">
window.onload = function() {
    document.form.answer.value = '1';
};
</script>

or better assign an id to your input and use this id:

<head>
<script type="text/javascript">
window.onload = function() {
    document.getElementById('answer').value = '1';
};
</script>
</head>
<body>
    <form name="form" action="enabled_catch.php" method="get">
        <input type="hidden" id="answer" name="answer" />
        <input type="submit" value="click me" />
    </form>
</body>

or even better use a javascript framework such jQuery to manipulate the DOM to ensure cross browser compatibility:

<script type="text/javascript">
$(function() {
    $(':hidden[name=answer]').val('1');
});
</script>
0

精彩评论

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