有没有办法使用javascript和JQuery添加一些使用POST从HTTP表单发送的其他字段?
我的意思是:
<form action="somewhere" method="POST" id="form">
<input type="submit" name="submit" value="Send" />
</form>
<script type="text/javascript">
$("#form").submit( function(eventObj) {
// I want to add a field "field" with value "value" here
// to the POST data
return true;
});
</script>
Run Code Online (Sandbox Code Playgroud) 我使用过去经常使用的方法发布一个非常简单的表单.显示我的代码可能更容易,而不是输入冗长的解释.这是HTML:
<% Html.BeginForm("CreateMarketingType", "ListMaintenance"); %>
<div id="ListMaintenanceContainer">
<table>
<tr>
<th>Marketing Type Id</th>
<th>Marketing Type Name</th>
</tr>
<%foreach (MarketingType marketingType in ViewData.Model.MarketingTypes) %>
<%{ %>
<tr>
<td><%= marketingType.MarketingTypeId.ToString() %></td>
<td><%= marketingType.MarketingTypeName %></td>
</tr>
<%} %>
</table>
<div>
<fieldset id="fsSaveNewMarketingType">
<legend>Add New Marketing Type</legend>
<label for="txtNewMarketingTypeName">New Marketing Type Name:</label>
<input type="text" id="txtNewMarketingTypeName" />
<input type="submit" value="Save" id="CreateMarketingType" />
</fieldset>
</div>
</div>
<% Html.EndForm();%>
Run Code Online (Sandbox Code Playgroud)
这是控制器代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateMarketingType(FormCollection form)
{
string newMarketingTypeName = Request.Form["txtNewMarketingTypeName"].ToString();
MarketingType newMarketingType = new MarketingType() { MarketingTypeName = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序(我的大学的一种社交网络).我需要添加注释(在特定数据库中插入一行).为此,我在html页面中有一个包含各种字段的HTML表单.在提交时我不使用表单的操作,但我在提交表单之前使用自定义javascript函数来详细说明一些数据.
function sendMyComment() {
var oForm = document.forms['addComment'];
var input_video_id = document.createElement("input");
var input_video_time = document.createElement("input");
input_video_id.setAttribute("type", "hidden");
input_video_id.setAttribute("name", "video_id");
input_video_id.setAttribute("id", "video_id");
input_video_id.setAttribute("value", document.getElementById('video_id').innerHTML);
input_video_time.setAttribute("type", "hidden");
input_video_time.setAttribute("name", "video_time");
input_video_time.setAttribute("id", "video_time");
input_video_time.setAttribute("value", document.getElementById('time').innerHTML);
oForm.appendChild(input_video_id);
oForm.appendChild(input_video_time);
document.forms['addComment'].submit();
}
Run Code Online (Sandbox Code Playgroud)
最后一行将表单提交到正确的页面.它工作正常.但我想使用ajax提交表单,我不知道如何做到这一点,因为我不知道如何捕获表单输入值.有人可以帮帮我吗?
在我的剃刀视图中,我正在使用Html.BeginForm.在其中我有两个元素,当我设置clic时,应该提交表单但我需要添加一个额外的字符串参数.
@using (Html.BeginForm("Index", "Quiz", FormMethod.Post, new { id = "form" }))
{
...
}
[HttpPost]
public ActionResult Index(string parameter, QuizCompletedViewModel q) //FormCollection f)
{
...
if (button.Equals("d"))
{
...
return RedirectToAction("ShowResults", new { testId = Quiz.QuizId, answeredTest = answeredId });
}
else
{
...
return RedirectToAction("Index", "Dashboard");
}
}
Run Code Online (Sandbox Code Playgroud)
因此,在我使用的jquery函数中$("#element").submit(),参数parameter始终为null(这是正常的).如何添加其他数据以parameter使用JQUERY?
注意:我没有使用AJAX.