문제

<form class="myform" action="mail.php">
Your name:<br>
<input type="text" name="myform-name"><br><br>
Your file:<br>
<input type="file" name="myform-file"><br><br>
<button type="submit">Submit</button>
</form>

How do I submit this form using vanilla javascript (not jQuery) directly from the code (without user interaction)?

도움이 되었습니까?

해결책

You can use:

document.getElementsByTagName('form')[0].submit()

다른 팁

Just add a form name in your code:

<form name="myform" class="myform" action="mail.php">
Your name:<br>
<input type="text" name="myform-name"><br>
<button type="submit">Submit</button>
</form>

submit the from from javascript:

<script type="text/javascript">document.myform.submit();</script>

Use this code

document.getElementById("my_form_id").submit();

Docs here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top