在我看来,我想要两个按钮来填充表单的三个隐藏字段并提交它。它不起作用,我不知道为什么。有任何想法吗?
我有以下代码:
<script language="javascript" type="text/javascript">
function FillAndSubmit(thisid) {
$('input[name=First]').val(thisid);
$('input[name=Second]').val(@Model.ID);
$('input[name=Third]').val(@ViewBag.Something);
document.form.submit();
}
</script>
A 有一个带有不可见字段的表单:
@using (Html.BeginForm(null, null, FormMethod.Post,
new { name = "form", id = "form" }))
{
@Html.Hidden("First")
@Html.Hidden("Second")
@Html.Hidden("Third")
}
还有两个按钮:
<button class="Button" id="ButtonA" onclick="javascript:FillAndSubmit(this.id);">
CaptionA
</button>
<button class="Button" id="ButtonB" onclick="javascript:FillAndSubmit(this.id);">
CaptionB
</button>
如果我将按钮放在表单范围内,它会提交但不填充隐藏字段。因此,它只是不调用该函数。Javascript 在同一应用程序的其他页面中运行。
添加:
生成源:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Title</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
function Botao(thisid) {
$('input[name=First]').val(thisid);
$('input[name=Second]').val(41);
$('input[name=Third]').val(10/5/2012 1:58:02 PM);
document.form.submit();
}
</script>
...
Some div's with some text
...
<form action="/Controller/Action/" id="form" method="post" name="form"><input id="First" name="First" type="hidden" value="" /><input data-val="true" data-val-required="The DateTime field is required." id="Second" name="Second" type="hidden" value="10/5/2012 1:58:02 PM" /><input id="Third" name="Third" type="hidden" value="" /></form>
<button class="Button" id="ButtonA" onclick="javascript:Botao(this.id);">
CaptionA
</button>
<button class="myButton" id="ButtonB" onclick="javascript:Botao(this.id);">
CaptionB
</button>
</body>
</html>