更改输入值然后在 JavaScript 中提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6912197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:44:05  来源:igfitidea点击:

Change value of input then submit form in JavaScript

javascripthtmlformsonclicksubmit

提问by Paparappa

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.

这就是我使用 JavaScript 的进展。它将“myinput”的值更改为 1 但不提交表单。我真的是一个 JavaScript 菜鸟,所以如果这只是一个太简单的问题,请原谅我,但我只是没有真正掌握它。

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

回答by valderman

You could do something like this instead:

你可以这样做:

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

And then modify your DoSubmit function to just return true, indicating that "it's OK, now you can submit the form" to the browser:

然后将你的 DoSubmit 函数修改为只返回 true,表示“没关系,现在你可以提交表单了”到浏览器:

function DoSubmit(){
  document.myform.myinput.value = '1';
  return true;
}

EDIT: I'd also be wary of using onclick events on a submit button; the order of events isn't immediately obvious, and your callback won't get called if the user submits by, for example, hitting return in a textbox.

编辑:我也对在提交按钮上使用 onclick 事件持谨慎态度;事件的顺序不是很明显,如果用户提交,例如,在文本框中点击返回,则不会调用您的回调。

回答by Claude Schlesser

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

This wont work as your form tag has no id.

这不会起作用,因为您的表单标签没有 id。

Change it like this and it should work:

像这样改变它,它应该可以工作:

<form name="myform" id="myform" action="action.php">

回答by Ashwin Krishnamurthy

No. When your input type is submit, you should have an onsubmitevent declared in the markup and then do the changes you want. Meaning, have an onsubmitdefined in your form tag.

不。当您的输入类型被提交时,您应该onsubmit在标记中声明一个事件,然后进行您想要的更改。意思是,onsubmit在你的表单标签中定义了一个。

Otherwise change the input type to a button and then define an onclickevent for that button.

否则,将输入类型更改为按钮,然后onclick为该按钮定义一个事件。

回答by Said Marar

here is simple code, you must set an id for your input here call it 'myInput' :

这是简单的代码,您必须在此处为输入设置一个 id,将其称为 'myInput' :

var myform = document.getElementById('myform');
myform.onsubmit = function(){ 
    document.getElementById('myInput').value = '1';
    myform.submit();
};   

回答by Chris Snowden

You're trying to access an element based on the nameattribute which works for postbacks to server but javascript responds to the idattribute. Add an idwith the same value as nameand all should work fine.

您正在尝试根据name用于回发到服务器的属性访问元素,但 javascript 会响应该id属性。添加一个id与相同的值name,一切都应该正常工作。

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

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

回答by Dinesh Rajan

My problem turned out to be that I was assigning as document.getElementById("myinput").Value = '1';

我的问题原来是我分配为 document.getElementById("myinput").Value = '1';

Notice the capital V in Value? Once I change it to small case ie; value, the data started posting. Odd as it was not giving any Javascript errors as well

注意到 Value 中的大写 V 了吗?一旦我将其更改为小案例,即;值,数据开始发布。奇怪,因为它也没有给出任何 Javascript 错误

回答by iCrazybest

Can you using onchangeEvent

你可以使用onchange事件吗

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

回答by Partab Saifuddin Zakir

This might help you.

这可能对你有帮助。

Your HTML

你的 HTML

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

YOUR SCRIPT

你的脚本

    <script>
        function save(){
            $('#myinput').val('1');
            $('#form').submit();
        }
    </script>