0

我需要验证我的 ASP MVC3 视图中的一些字段,并尝试使用 jQuery 来执行此操作。jQuery.val()方法可以很好地从输入字段中提取 、 和 ,但是该accountNumber字段refNumber的值始终为 。ownerMasteragentId""

我一遍又一遍地查看代码中的拼写错误,但没有找到。希望另一双眼睛能解决问题。

仅供参考 - 如果我将下面的.val()on更改为 hide 方法有效并且该字段消失,这意味着我输入了正确的选择器。$('#AgentId').val().hide()

jQuery

function ValidateFields(transactionType) {
    var msg = "";
    var refNumber = $('#RefNumber').val();
    var ownerMaster = $('#OwnerMasterAgentId').val();
    var accountNumber = $('#AccountNumber').val();
    var agentId = $('#AgentId').val(); //When the agentId variable an selector + val() are
                                       //evaluated here both have a value of ""

ASP MVC 剃刀

                <div class="M-editor-label">
                    @Html.LabelFor(model => model.RefNumber)
                    <span class="alwaysReq"> @Html.Raw("*")</span>
                </div>
                <div class="M-editor-field">
                    @Html.TextBoxFor(model => model.RefNumber, new { maxlength = 9, @class = "staticData" })
                </div>
                <div class="M-editor-label">
                    @Html.LabelFor(model => model.OwnerMasterAgentId)
                    <span class="req">@Html.Raw("*")</span> 
                </div>
                <div class="M-editor-field">
                    @Html.TextBoxFor(model => model.OwnerMasterAgentId, new { maxlength = 8, @class="staticData" })
                </div>
                    <div class="M-editor-label">
                        @Html.LabelFor(model => model.AccountNumber)
                        <span class="req">@Html.Raw("*")</span>
                    </div>
                    <div class="M-editor-field">
                        @Html.TextBoxFor(model => model.AccountNumber, new { maxlength = 17 })
                    </div>
                    <div class="M-editor-label">
                        @Html.LabelFor(model => model.AgentId)
                        <span class="req">@Html.Raw("*")</span>
                    </div>
                    <div class="M-editor-field">
                        @Html.TextBoxFor(model => model.AgentId, new { maxlength = 8 })
                    </div>

HTML

                <div class="M-editor-label">
                    <label for="RefNumber">Reference Number</label>
                    <span class="alwaysReq" style="display: inline;"> *</span>
                </div>
                <div class="M-editor-field">
                    <input class="staticData" id="RefNumber" maxlength="9" name="RefNumber" type="text" value="">
                </div>
                <div class="M-editor-label">
                    <label for="OwnerMasterAgentId">Owner Master Agent Id</label>
                    <span class="req" style="display: inline;">*</span> 
                </div>
                <div class="M-editor-field">
                    <input class="staticData" id="OwnerMasterAgentId" maxlength="8" name="OwnerMasterAgentId" type="text" value="">
                </div
                    <div class="M-editor-label">
                        <label for="AccountNumber">Account Number</label>
                        <span class="req" style="display: inline;">*</span>
                    </div>
                    <div class="M-editor-field">
                        <input id="AccountNumber" maxlength="17" name="AccountNumber" type="text" value="">
                    </div>
                    <div class="M-validator-field">
                        <span class="field-validation-valid" data-valmsg-for="AccountNumber" data-valmsg-replace="true" style="display: none;"></span>
                    </div>
                    <div class="M-editor-label">
                        <label for="AgentId">Agent ID</label>
                        <span class="req" style="display: inline;">*</span>
                    </div>
                    <div class="M-editor-field">
                        <input id="AgentId" maxlength="8" name="AgentId" type="text" value="">
                    </div>
4

2 回答 2

0

我没有看到问题,

  1. 但是你的页面中有没有其他 html 元素有 id=AgentId
  2. 当您更改字段的文本时,它是否会在萤火虫中发生变化

实际上,您不必创建自己的验证,您可以使用提供服务器端和客户端验证的数据注释验证

于 2013-08-20T22:43:39.477 回答
-1

尝试这个

var agentId=$('#@Html.FieldIdFor(model => model.AgentId)').val();
于 2013-08-21T05:09:38.947 回答