jQuery验证与”按钮"类型而不是"提交"键入一个表单 [英] JQuery validation with "button" type rather than "submit" type for a form

查看:108
本文介绍了jQuery验证与”按钮"类型而不是"提交"键入一个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('选择')。验证(),似乎要建更多比按钮类型输入类型按钮。我怎么会得到它与表单中的一个按钮类型的工作?

  @using(Html.BeginForm(新{ID =TheForm}))
{
     // code在这里
     <输入ID =TheButton类型=按钮>
}

JQuery的:

  $(文件)。就绪(函数(){
      $(#TheForm)。验证({
            规则:{
                 AuditDoc.Title:{
                       要求:真
                  }
            }
      });
      $(#TheButton)。点击(函数(){      });
});

使用这个基本的想法,我怎么会得到JQuery验证一个按钮的工作,而不是一个提交按钮类型?我见过的例子,jQuery将自动显示错误消息时,使用类型提交不符合规则,但它不会出现与按钮式的工作。我倒是AP preciate任何意见!


解决方案

  $(文件)。就绪(函数(){
        $(#TheForm)。验证({
            规则:{
                AuditDoc.Title:{
                    要求:真
                }
            }
        });        $(#TheButton)。点击(函数(){
            如果(!$(#TheForm)。验证()){//无效
                返回false;
            }其他{
                $(#TheForm)。提交()
            }
        });        $('#btnReset')。生活('点击',功能(E){
            //变量$ validateObj = $('#formId');
            //要么
            变量$ validateObj = $(本)。家长(形式);            $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();
        });
    });

$('selector').validation() seems to be built more for the "input" type button than the "button" type. How would I get it working with a button type within the form?

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

      });
});

Using this basic idea, how would I get jquery validate working with a button rather than a submit type button? I've seen examples where JQuery automatically displays error messages when the rules aren't met using submit type, but it doesn't appear to work with button type. I'd appreciate any advice!

解决方案

$(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();
        });
    });

这篇关于jQuery验证与&rdquo;按钮&QUOT;类型而不是&QUOT;提交&QUOT;键入一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆