简体   繁体   English

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

[英]Change value of input and submit form in JavaScript

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.这就是我对 JavaScript 代码的了解。 It changes "myinput"'s value to 1, but it does not submit the form.它将“myinput”的值更改为 1,但它不提交表单。

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

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

I'd also be wary of using onclick events on a submit button;我也会警惕在提交按钮上使用 onclick 事件; 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.事件的顺序不是很明显,如果用户提交,例如,在文本框中点击返回,您的回调将不会被调用。

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

This won't work as your form tag doesn't have an id.这不起作用,因为您的表单标签没有 id。

Change it like this and it should work:像这样更改它,它应该可以工作:

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

Here is simple code.这是简单的代码。 You must set an id for your input.您必须为您的输入设置一个 id。 Here call it 'myInput':这里称它为“myInput”:

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

No. When your input type is submit, you should have an onsubmit event declared in the markup and then do the changes you want.不,当您的输入类型是提交时,您应该在标记中声明一个onsubmit事件,然后进行您想要的更改。 Meaning, have an onsubmit defined in your form tag.意思是,在您的表单标签中定义一个onsubmit

Otherwise change the input type to a button and then define an onclick event for that button.否则将输入类型更改为按钮,然后为该按钮定义onclick事件。

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.您正在尝试访问基于name属性的元素,该属性适用于回发到服务器,但 JavaScript 响应id属性。 Add an id with the same value as name and all should work fine.添加一个与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;
}

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?注意 Value 中的大写 V 吗? Once I changed it to small case, ie, value , the data started posting.一旦我将其更改为小写,即value ,数据就开始发布。 Odd as it was not giving any JavaScript errors either.奇怪的是它也没有给出任何 JavaScript 错误。

You can use the onchange event:您可以使用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>

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>

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.首先,您必须添加一个脚本,例如我的 SetHolderParent() 并调用下面的 html 代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM