简体   繁体   English

即使在我的 javascript 调用提交之前,不显眼的验证也失败了

[英]Unobtrusive validation fails even before my javascript call to submit

I have an ASP.NET MVC application with a form defined as:我有一个 ASP.NET MVC 应用程序,其表单定义为:

@using (Ajax.BeginForm("Identify", "Employee", new AjaxOptions()
{
    HttpMethod = "POST",
    OnSuccess = "Identify.OnSuccess(data, status, xhr)",
    OnFailure = "Identify.OnFailure(xhr, status, error)"
}, new { id = "identifyForm"}))
{
    <div id="employeeIdContainer">
        @Html.LabelFor(m => m.IdEmployee) : <br/>
        @Html.TextBoxFor(m => m.IdEmployee, new {@type = "number", @Id = "IdEmployee"})
        <span class="validation">
        @Html.ValidationMessageFor(m => m.IdEmployee)
    </span>
    </div>

    <div id="pinContainer">
        @Html.LabelFor(m => m.Pin) : <br/>
        @Html.PasswordFor(m => m.Pin, new {@type = "number", @maxlength = "4", @Id = "Pin"})
        <span class="validation">
        @Html.ValidationMessageFor(m => m.Pin)
    </span>
    </div>

    <div>
        <input class="validate" type="submit" value="Submit" name="identifyButton"/>
    </div>

    <div id="keyboardContainer">
        <ul id="keyboard">
            <li class="number">1</li>
            <li class="number">2</li>
            <li class="number">3</li>
            <li class="validate">Submit</li>
            <li class="number line">4</li>
            <li class="number">5</li>
            <li class="number">6</li>
            <li class="delete">Corriger</li>
            <li class="number line">7</li>
            <li class="number">8</li>
            <li class="number">9</li>
            <li class="number line hidden"></li>
            <li class="number">0</li>
            <li class="number hidden"></li>
        </ul>   
    </div>
}

Inside the form I have a ul that I styled as a keyboard and this keyboard has an li that I want to use as a submit button, the one with the validate class.在表单内部,我有一个 ul,我将它设计为键盘,而这个键盘有一个 li,我想将其用作提交按钮,即带有验证类的按钮。 This isn't a regular submit button, but how do I submit the form in this case?这不是一个常规的提交按钮,但在这种情况下我如何提交表单? I tried the following in javascript:我在javascript中尝试了以下内容:

 $("#keyboard li.validate").click(function () {
    if ($("#identifyForm").valid()) {
        $("#identifyForm").submit();
    }
});

...but for some reason, before this javascript code is even called, the @Html.PasswordFor textbox gets erased and the validation kicks in saying that I need to enter a valid pin number (even when I just entered a valid one). ...但出于某种原因,在调用此 javascript 代码之前,@Html.PasswordFor 文本框被删除,验证开始说我需要输入一个有效的密码(即使我刚刚输入了一个有效的密码)。

I have jQuery code that updates the EmployeeId and Pin number as the user types in the keyboard.我有 jQuery 代码,可以在用户在键盘中键入时更新 EmployeeId 和 Pin 号。 I'm starting to think that the Unobtrusive validation mechanism does not see that these values have been updated and so it thinks that the Pin number is still empty.我开始认为 Unobtrusive 验证机制没有看到这些值已更新,因此它认为 Pin 号仍然为空。 Here is the jQuery code if it helps:如果有帮助,这里是 jQuery 代码:

var keyboard = $(function () {

var currentInput = $("#IdEmployee");

$("#Pin").on("focusin", function() {
    currentInput = $("#Pin");
});

$("#IdEmployee").on("focusin", function() {
    currentInput = $("#IdEmployee");
});

$('#keyboard li').click(function () {
    var $this = $(this),
        character = $this.html();

    if ($this.hasClass('delete')) {
        var html = currentInput.val();
        currentInput.val(html.substr(0, html.length - 1));
        return false;
    }
    currentInput.val(currentInput.val() + character);
});

$("#keyboard li.validate").click(function () {
    if ($("#identifyForm").valid()) {
        $("#identifyForm").submit();
    }
});

$("#IdEmployee").focus();

}); });

Your $('#keyboard li').click(function () { is setting the current input to the text value of the associated li element.您的$('#keyboard li').click(function () {正在将当前输入设置为关联li元素的文本值。

In the case of <li class="validate">Submit</li> it is setting the value of the current numeric input to the value "Submit" which is not a valid number, so validation fails.<li class="validate">Submit</li>的情况下,它将当前数字输入的值设置为不是有效数字的值"Submit" ,因此验证失败。 And because its invalid, the contents are cleared (that is the default behavior of the HTML5 control)并且因为它无效,所以内容被清除(这是 HTML5 控件的默认行为)

You can make this work by modifying your script to您可以通过将脚本修改为

$('#keyboard li').click(function () {
  var $this = $(this),
      character = $this.html();
  if ($this.hasClass('validate')) {
    return;
  } else if ($this.hasClass('delete')) {
    var html = currentInput.val();
    currentInput.val(html.substr(0, html.length - 1));
    return false;
  }
  currentInput.val(currentInput.val() + character);
});

or modify the selector to exclude the li with class="validate" element或修改选择器以排除带有class="validate"元素的li

$('#keyboard li:not(.validate)') {

Side note: Its not necessary to add new { @Id = "IdEmployee"} to your controls.旁注:没有必要将new { @Id = "IdEmployee"}到您的控件中。 Your just overwriting the id attribute with the value that it already is.您只需用它已经存在的值覆盖id属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM