简体   繁体   中英

Javascript doesn't work in html

My script:

<!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>

The javascript doesn't do its job that is clicking the submit button on page load, I can't figure out why, any ideas?

@ balintpekker still doesn't click the submit button on page load, my script:

<!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>

Look in your JavaScript error console. You will see it complaining that $ is undefined.

It looks like you are trying to use the jQuery library, but your forgot to load it .

You need to load the jQuery library or else the $ function will not work (edited to use jQuery syntax :) ) :

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

You are missing the form closing tag:

<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

You need to include the Jquery library to use the $(document) function. And you need to "close" the form tag.

Jquery library:

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

Closing form tag:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM