javascript javascript更改隐藏值然后提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11316117/
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-10-26 12:44:14  来源:igfitidea点击:

javascript to change hidden value then submit form

javascripthtmlforms

提问by Brds

(I've searched for something similar, but haven't found anything that matches and/or works)

(我搜索过类似的东西,但没有找到任何匹配和/或有效的东西)

I have a progress review form where the user reviews their settings from previous steps. Below is the form as I have it now:

我有一个进度表,用户可以在其中之前步骤中的设置。以下是我现在拥有的表格:

<form id='reviewForm' name='reviewForm' action='?action=ae&view=makeWorkFlow' method='post'>
    <table width='100%'>
        <tr>
            <th width='150'>Workflow Name</th>
            <td>" . $workflowName . "</td>
        </tr>
        <tr>
        <th valign='top'>Tasks</th>
            <td>
                <table width='99%'>
                    <tr>
                        <td>
                            <select name='task_1'>
                                <option value='0'>Select a Task</option>
                                <ALL OTHER OPTIONS>
                            </select>
                        </td>
                        <td><input type='text' name='turnAround_1' value='#VALUE#' onkeyup="if (/[a-z\s,$]/ig.test(this.value)) this.value = this.value.replace(/[a-z\s,$]/ig,'')" placeholder='Turn Around Time (hours)' /> Hours</td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan='2'>
                <input type='hidden' id='step' name='step' value='4' />
                <input type='submit' name='submit' value='Create Workflow' />&nbsp;&nbsp;
                <input type='button' value='Update Changes' onclick='setStep();' />&nbsp;&nbsp;
                <input type='button' value='Cancel' onclick='javascript:history.go(-3);' />
            </td>
        </tr>
    </table>
</form>

So, basically, the user has already selected items from a list of dropdowns. I need to show them a "review" page where they have the option to change their selections and click the "Update Changes" button. When the click that button, the value of the "step" hidden field needs to be changed from 4 to 3 and then submit the form via the "setStep" function, which is as follows:

因此,基本上,用户已经从下拉列表中选择了项目。我需要向他们展示一个“评论”页面,他们可以在其中更改选择并单击“更新更改”按钮。当点击那个按钮时,需要将“step”隐藏字段的值从4改为3,然后通过“setStep”函数提交表单,如下:

<script language='JavaScript' type='text/javascript'>
    function setStep()
    {
        document.forms["reviewForm"].step.value = 3;
        var t = setTimeout("document.reviewForm.submit();", 0);
    }
</script>

I realize that the setTimeout isn't the best way to do it, but it's just where I'm out now in trying to figure this out. I'm sure it's some small thing i'm missing that would fix this, but I've been at this for just over an hour and can't find it.

我意识到 setTimeout 不是最好的方法,但这正是我现在试图解决这个问题的地方。我确定这是我遗漏的一些小东西可以解决这个问题,但我已经在这里呆了一个多小时却找不到它。

回答by swapnilsarwe

The issue is that the name of the submit button is submit

问题是提交按钮的名称是 submit

Hence while resolving document.reviewForm.submit()

因此在解决 document.reviewForm.submit()

It does not allow since submit() is the function in javascript. And in your code submit is also the name of the element (<input type='submit' name='submit' value='Create Workflow' />) in the form.

它不允许,因为 submit() 是 javascript 中的函数。在您的代码中 submit 也是<input type='submit' name='submit' value='Create Workflow' />表单中元素 ( )的名称。

change the name of the button to anything else than submit(for eg: submit1) and it will work successfully

将按钮的名称更改为submit(例如:)以外的任何名称,submit1它将成功运行

Also no need to us setTimeoutsimply use the following code

也不需要我们setTimeout简单地使用以下代码

function setStep()
    {
        document.forms["reviewForm"].step.value = 3;
                document.reviewForm.submit();
    }

回答by MaVRoSCy

document.getElementById('step').value='3';
document.getElementById('reviewForm').submit();

UPDATE

更新

回答by RandomDuck.NET

Try document.getElementById('step').value = 3instead of document.forms["reviewForm"].step.value = 3;.

尝试document.getElementById('step').value = 3代替document.forms["reviewForm"].step.value = 3;.