在 JavaScript 中更改输入值并提交表单
我目前正在制作一个基本表格。当您点击提交按钮时,它应该首先更改字段的值,然后像平常一样提交表单。这一切看起来有点像这样:
<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>
这就是我在 JavaScript 代码方面的进展。它将“myinput”的值更改为 1,但不提交表单。
function DoSubmit(){
document.myform.myinput.value = '1';
document.getElementById("myform").submit();
}
I'm currently working on a basic form. When you hit the submit button, it should 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 code. It changes "myinput"'s value to 1, but it does not submit the form.
function DoSubmit(){
document.myform.myinput.value = '1';
document.getElementById("myform").submit();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不可以。当您的输入类型为提交时,您应该在标记中声明一个
onsubmit
事件,然后进行您想要的更改。意思是,在表单标记中定义一个onsubmit
。否则,将输入类型更改为按钮,然后为该按钮定义一个
onclick
事件。No. When your input type is submit, you should have an
onsubmit
event declared in the markup and then do the changes you want. Meaning, have anonsubmit
defined in your form tag.Otherwise change the input type to a button and then define an
onclick
event for that button.您尝试访问基于
name
属性的元素,该属性用于回发到服务器,但 JavaScript 响应id
属性。添加一个与name
具有相同值的id
,一切都应该可以正常工作。You're trying to access an element based on the
name
attribute which works for postbacks to the server, but JavaScript responds to theid
attribute. Add anid
with the same value asname
and all should work fine.我的问题原来是我分配为
document.getElementById("myinput").Value = '1';
注意到 Value 中的大写 V 了吗?一旦我将其更改为小写,即 value,数据就开始发布。奇怪的是它也没有给出任何 JavaScript 错误。
My problem turned out to be that I was assigning as
document.getElementById("myinput").Value = '1';
Notice the capital V in Value? Once I changed it to small case, i.e., value, the data started posting. Odd as it was not giving any JavaScript errors either.
我已经做到了这一点并且它对我有用。
首先,您必须添加一个脚本,例如我的 SetHolderParent() 并调用如下所示的 html 代码。
I have done this and it works for me.
At first you must add a script such as my SetHolderParent() and call in the html code like below.
您可以使用 onchange 事件:
You can use the onchange event:
这可能对你有帮助。
您的 HTML
您的脚本
This might help you.
Your HTML
Your Script
您可以这样做:
然后修改您的 DoSubmit 函数以仅返回 true,表示“没关系,现在您可以将表单提交”到浏览器:
我还对在提交按钮上使用 onclick 事件持谨慎态度;事件的顺序并不是很明显,并且如果用户通过(例如在文本框中按回车键)提交,则不会调用您的回调。
You could do something like this instead:
And then modify your DoSubmit function to just return true, indicating that "it's OK, now you can submit the form" to the browser:
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.
这不起作用,因为您的表单标签没有 id。
像这样改变它,它应该可以工作:
This won't work as your form tag doesn't have an id.
Change it like this and it should work:
这是简单的代码。您必须为您的输入设置一个 id。这里将其称为“myInput”:
Here is simple code. You must set an id for your input. Here call it 'myInput':