javascript - 使用 javascript 提交具有不同值的表单

标签 javascript html forms

我对表单和 JavaScript 很陌生,所以请原谅。

我有一个如下所示的表单:

<form method="post" action="control" name="myform">
<input type="submit" name="Name" value="Do A"/>
<input type="submit" name="Name" value="Do B" />
<input type="submit" name="Name" value="Do C" />
<input type="submit" name="Name" value="Close" />
</form>

我需要对此进行更改,以便有两个按钮,并且表单是根据某些外部条件使用 javascript 提交的,因此我希望它看起来像这样:

<form method="post" action="control" name="myform">
<script>
function submitForm(form){
    if(someConditionA){
        submit the form so that the script performs same action as if the button Do A had been clicked
    } if (someConditionB){
        submit the form so that the script performs same action as if the button Do B had been clicked
    } if (someConditionC){
        submit the form so that the script performs same action as if the button Do C had been clicked
    }
}

function closeForm(form){
    window.close();
}
</script>
<button name="doAction" onClick="SubmitForm(this.form)">Do Action<\button>
<button name="close" onClick="SubmitForm(this.form)">Close<\button>
</form>

如何实现submitForm函数?

谢谢

最佳答案

添加一个与原始提交按钮同名的隐藏字段:

<input type="HIDDEN" name="Name" value=""/>

根据您的条件设置该字段的值:

function submitForm(form){
    if(someConditionA){
        form.Name.value = "Do A";
    } if (someConditionB){
        form.Name.value = "Do B";
    } if (someConditionC){
        form.Name.value = "Do C";
    }
    form.submit();
}

将新的关闭按钮更改为:

<button name="close" onClick="this.form.Name.value='Close';this.form.submit();">Close<\button>

我还没有对此进行测试,因此它可能包含一两个错误,但这是总体思路。 (为“this.form”+1,没有多少人知道这一点,很好。)

关于javascript - 使用 javascript 提交具有不同值的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903964/

相关文章:

javascript - 在 Woocommerce 中交换图像变化的问题

javascript - 中心推特嵌入 iframe

javascript - 在 <form> 之外输入标签并提交表单

javascript - div onclick 表单未在 javascript 中提交

django - 检查 Django request.POST 中的内容

javascript - 在同构 React 应用程序中,有哪些减少 FID(首次输入延迟)的好方法?

javascript - <img> 的 XSS 上的数据 URI 是否可被利用?

javascript - 为无序项目分配编号

javascript - 浏览器如何识别html中的angularjs标签?

jquery .each 仅适用于第一个元素