用于自动的JavaScript单击提交按钮

时间:2018-03-02 18:45:32

标签: javascript

请提供JavaScript以自动点击提交按钮:  以下是示例表单:“表单”和“按钮”中没有“id”标记

<form action="?" method="post" >                    

     <div style="display: inline-block;">
        <input type="submit" value="Claim" >
    </div>   
 </form>

1 个答案:

答案 0 :(得分:0)

只需使用按钮的.click()方法:

// This code is just here to demonstrate the the submit button will
// wind up getting its click event triggerd and because it is a submit
// button, that will trigger the submit event of the form.
document.querySelector("form").addEventListener("submit", function(evt){
  evt.preventDefault();
  alert("You triggered the click event of the button!");
});

// Just call the .click method of the button
document.querySelector("input[type='submit']").click();
<form action="?" method="post" >                    
 <div style="display: inline-block;">
   <input type="submit" value="Claim" >
 </div>   
</form>

但是,如果您的真正目标是触发表单的submit事件,则可以绕过提交按钮的click事件并直接提交表单:

document.querySelector("form").submit();