我有以下javascript将表单加载到页面上的隐藏div中并显示div
function load() {
...stuff to get date and id...
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (xhttp.readyState==4&&xhttp.status==200) {
document.getElementById("PopupDiv").innerHTML = xhttp.responseText;
document.getElementById("PopupDiv").removeAttribute("hidden");
alert(xhttp.responseText);
}
};
xhttp.open("GET", "Object?date=" + date + "&id=" + id, true);
xhttp.send();
}
这会加载部分视图,其中包含以下内容:
<h2>@Model.date</h2>
<h2>@Model.id</h2>
<div id="OptionsDiv">
@using (Html.BeginForm("Confirm", "Plan",FormMethod.Post,new { id = "OptionsForm" }))
{
@Html.HiddenFor(x => x.date)
@Html.HiddenFor(x => x.id)
@Html.LabelFor(x => x.options)
@Html.DropDownListFor(x => x.options, Model.optionsDropdown)
<br />
@Html.LabelFor(x => x.comment)
@Html.TextBoxFor(x => x.comment)
<br />
<input type="submit" value="Submit"/>
}
</div>
<p>test after the div</p>
当触发javascript事件时,在查看DOM资源管理器时它都被正确加载,但表单未显示。显示之前和之后的信息,但表格中没有任何内容。
来自回复的警报包含:
<h2>5/12/2016 12:00:00 AM</h2>
<h2>13</h2>
<div id="OptionsDiv">
<form action="/Plan/Confirm" id="PlanOptionsForm" method="post"><input data-val="true" data-val-date="The field date must be a date." data-val-required="The date field is required." id="date" name="date" type="hidden" value="5-12-2016" /><input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="13" /><label for="Options">options</label><select data-val="true" data-val-number="The field meal must be a number." data-val-required="The meal field is required." id="meal" name="meal"><option selected="selected" value="0">--Choose an Option--</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select> <br />
<label for="comment">comment</label><input data-val="true" data-val-number="The field servings must be a number." data-val-required="The comment field is required." id="comment" name="comment" type="text" value="0" /> <br />
<input type="submit" value="Submit"/>
</form></div>
<p>test after the div</p>
如果我进入DOM浏览器并选择不同的部分,它们会显示出来,但初始加载看起来像:
这一切都在Chrome和Firefox中正确显示
此外,删除页面上的其他表单并不能解决问题。
答案 0 :(得分:5)
显然,您无法将表单加载到隐藏元素中。
我改变了我的JavaScript函数中的顺序:
document.getElementById("PopupDiv").innerHTML = xhttp.responseText;
document.getElementById("PopupDiv").removeAttribute("hidden");
要:
document.getElementById("PopupDiv").removeAttribute("hidden");
document.getElementById("PopupDiv").innerHTML = xhttp.responseText;
现在可行。