Javascript 不适用于 html

Javascript doesn't work in html

我的脚本:

<!DOCTYPE html>
<html>
<head>
<script>
$(document).ready(function () {
   document.getElementById("saveForm").click();
});
</script>
</head>
<!--<body onload="document.getElementById('saveForm').click();">-->
<body>

<form method="post" enctype="multipart-form-data" name="my_form" onsubmit="clearTextBoxCounter()" action="http://www.sms-online.web.id/kirim" >

  <input type=hidden name=teks value=><center><b>KIRIM SMS GRATIS</b></center><br><br>
Nomer HP:<br />
  <input class="field text small" type="text" maxlength="20" name="Phonenumbers" value="085999999"/>
  <br />

<br />
Isi Pesan:<br />
  <textarea rows="5" cols="20" onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=Text >sms content</textarea>
<br />

<input id="saveForm" class="btTxt" type="submit" value="KIRIM" name="TOMBOL" />

</body>
</html>

javascript 没有完成它在页面加载时单击提交按钮的工作,我不明白为什么,有什么想法吗?

@balintpekker 页面加载时仍然没有点击提交按钮,我的脚本:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
   document.getElementById("saveForm").click();
});
</script>
</head>
<!--<body onload="document.getElementById('saveForm').click();">-->
<body>

<form method="post" enctype="multipart-form-data" name="my_form" onsubmit="clearTextBoxCounter()" action="http://www.sms-online.web.id/kirim" >

  <input type=hidden name=teks value=><center><b>KIRIM SMS GRATIS</b></center><br><br>
Nomer HP:<br />
  <input class="field text small" type="text" maxlength="20" name="Phonenumbers" value="08555555"/>
  <br />

<br />
Isi Pesan:<br />
  <textarea rows="5" cols="20" onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=Text >testing pesan 4</textarea>
<br />

<input id="saveForm" class="btTxt" type="submit" value="KIRIM" name="TOMBOL" />

</body>
</html>

查看您的 JavaScript 错误控制台。您会看到它抱怨 $ 未定义。

您似乎正在尝试使用 jQuery 库,但您忘记了 load it

您需要包含 Jquery 库才能使用 $(document) 函数。并且您需要 "close" 表单标签。

Jquery 图书馆:

<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

关闭表单标签:

<input id="saveForm" class="btTxt" ...... />
</form><!-- closing TAG here -->
</body>
</html>

您需要加载 jQuery 库,否则 $ 函数将无法工作(编辑为使用 jQuery 语法 :)):

<head>
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    <script>
        $(function () {
            $("#saveForm").click();
        });
    </script>
</head>

您缺少表单结束标记:

<input id="saveForm" class="btTxt" type="submit" value="KIRIM" name="TOMBOL" />
</form><!-- missing here -->
</body>
</html>

HTML

<form method="post" enctype="multipart-form-data" 
    name="my_form" onsubmit="clearTextBoxCounter()" 
    action="http://www.sms-online.web.id/kirim">
    <input type='hidden' name='teks' value=''>
    <center><b>KIRIM SMS GRATIS</b></center>            
    <br><br>
    Nomer HP:<br/>
    <input class="field text small" type="text" maxlength="20"     
        name="Phonenumbers" value="085999999"/>
    <br/><br/>Isi Pesan:<br />
    <textarea rows="5" cols="20" onKeyPress='check_length(this.form)'
        onKeyDown='check_length(this.form)' name='Text'>sms content</textarea>
    <br/>
    <input id="saveForm" class="btTxt" type="submit" 
        value="KIRIM" name="TOMBOL" />
</form>

JS

document.getElementById("saveForm").click();

FIDDLE