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

发布于 2024-11-28 00:22:24 字数 541 浏览 1 评论 0原文

我目前正在制作一个基本表格。当您点击提交按钮时,它应该首先更改字段的值,然后像平常一样提交表单。这一切看起来有点像这样:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

魔法唧唧 2024-12-05 00:22:25

不可以。当您的输入类型为提交时,您应该在标记中声明一个 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 an onsubmit defined in your form tag.

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

夜未央樱花落 2024-12-05 00:22:25

您尝试访问基于 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;
}

You're trying to access an element based on the name attribute which works for postbacks to the server, but JavaScript responds to the id attribute. Add an id with the same value as name and all should work fine.

<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;
}
苏大泽ㄣ 2024-12-05 00:22:25

我的问题原来是我分配为 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.

似狗非友 2024-12-05 00:22:25

我已经做到了这一点并且它对我有用。
首先,您必须添加一个脚本,例如我的 SetHolderParent() 并调用如下所示的 html 代码。

function SetHolderParent(value) {
   alert(value);
}
<input type="submit" value="Submit" onclick="SetHolderParent(222);" />

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.

function SetHolderParent(value) {
   alert(value);
}
<input type="submit" value="Submit" onclick="SetHolderParent(222);" />

守不住的情 2024-12-05 00:22:25

您可以使用 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>

You can use the 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>
慕烟庭风 2024-12-05 00:22:25

这可能对你有帮助。

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

您的脚本

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

This might help you.

Your 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>
原来是傀儡 2024-12-05 00:22:24

您可以这样做:

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

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

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

我还对在提交按钮上使用 onclick 事件持谨慎态度;事件的顺序并不是很明显,并且如果用户通过(例如在文本框中按回车键)提交,则不会调用您的回调。

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:

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

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.

烟─花易冷 2024-12-05 00:22:24
document.getElementById("myform").submit();

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

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

<form name="myform" id="myform" action="action.php">
document.getElementById("myform").submit();

This won't work as your form tag doesn't have an id.

Change it like this and it should work:

<form name="myform" id="myform" action="action.php">
层林尽染 2024-12-05 00:22:24

这是简单的代码。您必须为您的输入设置一个 id。这里将其称为“myInput”:

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

Here is simple code. You must set an id for your input. Here call it 'myInput':

var myform = document.getElementById('myform');
myform.onsubmit = function(){
    document.getElementById('myInput').value = '1';
    myform.submit();
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文