gpt4 book ai didi

javascript - 在运行时使用 Html.BeginForm

转载 作者:行者123 更新时间:2023-12-03 12:24:00 25 4
gpt4 key购买 nike

我想有条件地使用 Html.BeginForm。代码中有2个字段如下

@using (Html.BeginForm("Form1Action", "Form1", FormMethod.Post, new { id = "form1" }))
{

<label for="firstlabel">Label 1</label>
<input type="text" id="firstlabel" name="firstlabel"/>

<label for="secondlabel">Label 2</label>
<input type="text" id="secondlabel" name="secondlabel"/>
<a href="#" class="button" onclick="SubmitForm();">Submit</a>
}

JS 是

<script type="text/javascript">
function SubmitForm() {
$('#form1').submit();
}
</script>

如果第二个标签为空或为空,那么我想通过另一个操作提交表单,那么如何才能实现这一点?

我的意思是另一个 Action :

 @using (Html.BeginForm("Form2Action", "Form2", FormMethod.Post, new { id = "form2" }))

最佳答案

试试这个..

function SubmitForm() {
if($('#secondlabel').val().length)
$('#form1').submit();
else
$('#form2').submit();
}

或者通过改变 Action

    function SubmitForm() {
if($('#secondlabel').val().length == 0)
$('#form1').attr('action', $('#form2').attr('action'));


$('#form1').submit();
}

关于javascript - 在运行时使用 Html.BeginForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24305175/

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