Change value of input then submit form in JavaScri

2019-01-07 12:49发布

I'm currently working on a basic form. What I want to do with this form is that when you hit submit/button to first change the value of a field and then submit the form as usual. It all looks a bit like this:

<form name="myform" id="myform" action="action.php">
<input type="hidden" name="myinput" value="0" />
<input type="text" name="message" value="" />
<input type="submit" name="submit" onclick="DoSubmit()" />
</form>

And this is how far I've come with the JavaScript. It changes "myinput"'s value to 1 but does not submit the form. I'm really a JavaScript noobie so forgive me if this is just a too simple question but I'm just not getting the hang of it really.

function DoSubmit(){
  document.myform.myinput.value = '1';
  document.getElementById("myform").submit();
}

7条回答
萌系小妹纸
2楼-- · 2019-01-07 13:47

Can you using onchange Event

<form name="myform" id="myform" action="action.php">
<input type="hidden" name="myinput" value="0" onchange="this.form.submit()"/>
<input type="text" name="message" value="" />
<input type="submit" name="submit" onclick="DoSubmit()" />
</form>
查看更多
登录 后发表回答