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 noobie,请原谅我,如果这只是一个太简单的问题,但我真的没有得到它。
function DoSubmit(){
document.myform.myinput.value = '1';
document.getElementById("myform").submit();
}
7 个解决方案
#1
46
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事件;事件的顺序并不是很明显,如果用户提交,例如在文本框中点击返回,则不会调用您的回调。
#2
5
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">
#3
5
here is simple code , you must set an id for you input here call it 'myInput' :
这里是简单的代码,你必须为你输入一个id输入这里称之为'myInput':
var myform = document.getElementById('myform');
myform.onsubmit = function(){
document.getElementById('myInput').value = '1';
myform.submit();
};
#4
5
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 an onsubmit
defined in your form tag.
不。当您的输入类型是提交时,您应该在标记中声明一个onsubmit事件,然后进行所需的更改。意思是,在表单标记中定义了一个onsubmit。
Otherwise change the input type to a button and then define an onclick
event for that button.
否则,将输入类型更改为按钮,然后为该按钮定义onclick事件。
#5
2
You're trying to access an element based on the name
attribute which works for postbacks to server but javascript responds to the id
attribute. Add an id
with the same value as name
and all should work fine.
您正在尝试访问基于name属性的元素,该属性适用于回发到服务器但javascript响应id属性。添加一个与name名称相同的id,所有这些都应该可以正常工作。
<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;
}
#6
1
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
注意资本V的价值?一旦我把它改成小盒子即;价值,数据开始发布。奇怪,因为它也没有给出任何Javascript错误
#7
0
Can you using onchange Event
你能使用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>
#1
46
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事件;事件的顺序并不是很明显,如果用户提交,例如在文本框中点击返回,则不会调用您的回调。
#2
5
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">
#3
5
here is simple code , you must set an id for you input here call it 'myInput' :
这里是简单的代码,你必须为你输入一个id输入这里称之为'myInput':
var myform = document.getElementById('myform');
myform.onsubmit = function(){
document.getElementById('myInput').value = '1';
myform.submit();
};
#4
5
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 an onsubmit
defined in your form tag.
不。当您的输入类型是提交时,您应该在标记中声明一个onsubmit事件,然后进行所需的更改。意思是,在表单标记中定义了一个onsubmit。
Otherwise change the input type to a button and then define an onclick
event for that button.
否则,将输入类型更改为按钮,然后为该按钮定义onclick事件。
#5
2
You're trying to access an element based on the name
attribute which works for postbacks to server but javascript responds to the id
attribute. Add an id
with the same value as name
and all should work fine.
您正在尝试访问基于name属性的元素,该属性适用于回发到服务器但javascript响应id属性。添加一个与name名称相同的id,所有这些都应该可以正常工作。
<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;
}
#6
1
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
注意资本V的价值?一旦我把它改成小盒子即;价值,数据开始发布。奇怪,因为它也没有给出任何Javascript错误
#7
0
Can you using onchange Event
你能使用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>