0

我找到了一些关于这个问题的主题,但没有一个适合我的需求,或者解决我的问题。所以这是主页上的:

<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()) {

它有效,但它也验证了主窗口上的表单,这是我目前不想要的。

4

2 回答 2

1

将表单最后的按钮更改为输入类型Submit,因为在尝试提交表单时会触发不显眼的验证。

还要确保

    <add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

这些键存在于 Web 配置中。并且在加载表单之前加载所有必需的脚本。

检查捆绑包 @section Scripts { @Scripts.Render("~/bundles/jqueryval") } 已被渲染。

于 2013-09-05T12:20:59.110 回答
0

问题解决了:

<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>
        </form>
        <div id="mydiv">
            @Html.Partial("~/Views/Orders/OrderItemAddPopup.cshtml", Model)
        </div>
    </div>
</div>

在弹出窗口内:

 <form id="#orderitemsform">
        @Html.ValidationSummary()
        <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>

和脚本:

$('#load').click(function () {
    $("#mydiv form").validate(); if ($("#mydiv form").valid()) {
        alert("valid");
    }
    else {
        alert("invalid");
    }
});
于 2013-09-05T12:30:37.853 回答