我找到了一些关于这个问题的主题,但没有一个适合我的需求,或者解决我的问题。所以这是主页上的:
<div id="contentDiv">
<div style="margin-top: -31px;">
<form>
<table class="tableFormLayout" cellpadding="0" cellspacing="0">
<tr>
<td>
<input type="button" id="openOrderItemAddPopup" value="Add" />
</td>
</tr>
</table>
@Html.Partial("~/Views/Orders/OrderItemAddPopup.cshtml", Model)
</form>
</div>
</div>
这在弹出窗口中:
@model MvcAppMobileJQuery.ViewModels.OrderVM
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<div data-role="none" id="OrderItemAddPopup" data-overlay-theme="b" style="width: 500px;"
class="ui-corner-all">
<div data-role="content">
@Html.ValidationSummary()
<form id="#orderitemsform">
<table class="tableFormLayout" cellpadding="0" cellspacing="0">
<tr>
<td>
@Html.LabelFor(m => m.Quantity, new {@class = "label"})
</td>
<td>
@Html.TextBoxFor(m => m.Quantity, new {data_mini = "true", type = "number", id = "txtQuantity"})
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" id="load" data-inline="true" value="Save" data-icon="forward"/>
</td>
</tr>
</table>
</form>
</div>
</div>
<script type="text/javascript">
$('#load').click(function () {
$('#orderitemsform').validate();
if ($('#orderitemsform').valid()) {
alert("valid");
}
else {
alert("invalid");
}
});
$(function () {
$('#OrderItemAddPopup').modalPopLite({ openButton: '#openOrderItemAddPopup', closeButton: '#closeOrderItemAddPopup', isModal: true });
});
</script>
和视图模型:
public class OrderVM
{
[DisplayName("Quantiy")]
[Required(ErrorMessage = "Quantity is required")]
[RegularExpression("^[1-9]\\d*$", ErrorMessage = "Quantity must be positive")]
public int Quantity { get; set; }
}
问题是我在控制台中出现错误:Uncaught TypeError: Cannot read property 'form' of undefined
如果我改变这个:
$("#orderitemsform").validate();
if ($("#orderitemsform").valid()) {
对此:
$('form').validate();
if ($('form').valid()) {
它有效,但它也验证了主窗口上的表单,这是我目前不想要的。