向表单提交添加其他参数 [英] Adding additional parameter to form submit
问题描述
我在Razor视图中有一个表单声明
I have a form declaration in my Razor view
<form method="post" action="/Lot/LotList?auctionEventId=@auctionEventId" id="filterForm">
(顺便说一句,我选择像这样写出来,而不是使用 Html.BeginFrom
,因为我需要给它一个ID,并且不知道如何使用 Html.BeginFrom
-但这不是问题所在
(Incidentally, I chose to write it out like this rather than use Html.BeginFrom
because I needed to give it an id and didn't know how to do that with Html.BeginFrom
- but that is not the issue here)
在此表单之外,我有一个提交此表单的按钮(表单中还有一个提交按钮)
Outside of this form I have a button which submits this form (there is also a submit button in the form)
<input type="button" onclick="$('#filterForm').submit();" value="Show all" />
现在,问题是,如果使用此按钮提交表单,我希望将表单中的操作更改为
Now, the issue is that if this button is used to submit the form I want the action in the form to change to
action="/Lot/LotList?auctionEventId=@auctionEventId&showAll=true"
如何更改操作并传递此附加参数?有没有更好的方法来完成所有这些工作?
How do I alter the action and pass this additional parameter? Is there a totally better way of doing all this?
推荐答案
将查询字符串参数附加到form操作上,并试图在运行时更改它是棘手的(但并非不可能).使用隐藏字段要容易得多:
Appending query string parameters onto the form action, and trying to change that at runtime is tricky (but not impossible). Far easier is to use hidden fields:
<form method="post" action="/Lot/LotList" id="filterForm">
<input type="hidden" name="auctionEventId" value="@auctionEventId" />
...
所以现在您要做的就是为"showAll"添加另一个隐藏字段
So now all you have to do is add another hidden field for "showAll"
<form method="post" action="/Lot/LotList" id="filterForm">
<input type="hidden" name="auctionEventId" value="@auctionEventId" />
<input type="hidden" name="showAll" value="false" id="showAllField" />
...
只需在showAll按钮上连接一个jquery事件:
And just hook up a jquery event on your showAll button:
<input id="showAllButton" type="button"/>
jQuery:
$('#showAllButton').click(function(){
$('#showAllField').val("true");
$('#filterForm').submit();
});
这篇关于向表单提交添加其他参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!