gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:44:55 25 4
gpt4 key购买 nike

我对表单和 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com