繁体   English   中英

使用“按钮”类型的JQuery验证而不是表单的“提交”类型

[英]JQuery validation with “button” type rather than “submit” type for a form

$('selector')。validation()似乎是为“输入”类型按钮而不是“按钮”类型构建的。 如何使用表单中的按钮类型?

@using(Html.BeginForm(new {id="TheForm"}))
{
     // code here
     <input id="TheButton" type="button">
}

JQuery的:

$(document).ready(function() {
      $("#TheForm").validate({
            rules: {
                 "AuditDoc.Title": {
                       required: true
                  }
            }
      });
      $("#TheButton").click(function() {

      });
});

使用这个基本思想,我如何让jquery验证使用按钮而不是提交类型按钮? 我已经看到了当使用提交类型不满足规则时JQuery自动显示错误消息的示例,但它似乎不适用于按钮类型。 我很感激任何建议!

$(document).ready(function () {
        $("#TheForm").validate({
            rules: {
                "AuditDoc.Title": {
                    required: true
                }
            }
        });

        $("#TheButton").click(function () {
            if (!$("#TheForm").validate()) { // Not Valid
                return false;
            } else {
                $("#TheForm").submit()
            }
        });

        $('#btnReset').live('click', function (e) {
            //var $validateObj = $('#formId');
            //Or
            var $validateObj = $(this).parents('form');

            $validateObj.find("[data-valmsg-summary=true]").removeClass("validation-summary-errors").addClass("validation-summary-valid").find("ul").empty();
            $validateObj.find("[data-valmsg-replace]").removeClass("field-validation-error").addClass("field-validation-valid").empty();
        });
    });
$(document).ready(function(){

     $("#myform").validate();
     $("#byuttion").click(function() {

       if($("#myform").valid())
       {
          $("#myform").submit();
       }
       else 
      {
          return false;
      }

      });

});

一种hackish方式可能是:

$("#TheButton").click(function() {
     $("#TheForm").submit()
});

除了不支持submit方法外,它与普通输入的工作原理基本相同。 preventDefault只是阻止页面执行默认请求。

$("#TheButton").click(function(event) {
    event.preventDefault(); //or return false
    getData();
});

暂无
暂无

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

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