表单验证只能使用type = button但不能使用type = submit吗?

时间:2014-01-28 04:40:44

标签: javascript jquery forms validation form-submit

我试图弄清楚为什么此验证脚本仅使用按钮 type =“button”而不是 type =“submit”,请参阅下面的代码,我尝试了不同版本的jquery,但没有运气..我猜它与checkMandatoryFieldsAndSubmit有关

谢谢!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<style>
.unset{
    border: 1px solid red
}
</style>
<body>

<div style="color: # 666; display: none; font-family:Arial, Helvetica, sans-serif; font-size:10px; text-align:center;"  id="error">Complete all highlighted mandatory fields</div> 

    <form action="http://www.aol.com" method="post">
    <input name="carname" type="text" value="" class="mandatory_field" />
    <button type="button" onclick="checkMandatoryFieldsAndSubmit()"/> Submit </button>
    </form>

</body>
</html>

<script>
function checkMandatoryFieldsAndSubmit(){
var error = false;
 $.map($('.mandatory_field'), function(element){
    //error = false;

    if ($(element).attr('type') == "text"){
      if ($(element).val() == ""){
        error = true;
        $(element).parent().addClass('unset');
      }else{
        $(element).parent().removeClass('unset');
      }
    }

    if ($(element).attr('type') == "checkbox"){    
      if ($(element).attr('checked') == false){
        error = true;
        $(element).parent().addClass('unset');
      }else{
        $(element).parent().removeClass('unset');
      }
    }

    if ($(element).attr('type') == "radio"){
      var radio_error = true;

      //check all having the same name!
      var name = $(element).attr('name');
      $('input[name="'+name+'"]').each(function(index){
        if (radio_error){
          if ($(this).attr('checked') == false){           
            radio_error = true;
          }else{
            radio_error = false;
          }
        }
      });

      if (radio_error){
        error = true;
        $(element).parent().addClass('unset');
      }else{
        $(element).parent().removeClass('unset');
      }
    }       
  });

  if (!error){
    $('name=')
    $('#form').submit();
  }else{
    $('#error').show();
  }
}

function addValue(check, element, value){
  //if checked will add value to the hidden 
  if ($(check).is(':checked')){
    checkVal = $(check).val();
    val = $('#'+element).val();
    if (val.indexOf(checkVal) == -1){
      val += ' '+checkVal;
      $('#'+element).val(val);
    }
  }
  //if checked will add value to the hidden 
  else{
    checkVal = $(check).val();
    val = $('#'+element).val();
    if (val.indexOf(checkVal) != -1){
      val = val.replace(checkVal, '');
      $('#'+element).val(val);
    }
  }
}
</script>

1 个答案:

答案 0 :(得分:3)

如果您使用的是type =“submit”,那么如果验证失败,您需要通过从验证方法返回false来停止默认操作。并将事件处理程序更改为

<button type="submit" onclick="return checkMandatoryFieldsAndSubmit()"/> Submit </button>

然后

function checkMandatoryFieldsAndSubmit() {
    var error = false;
    $.map($('.mandatory_field'), function (element) {
        //error = false;

        if ($(element).attr('type') == "text") {
            if ($(element).val() == "") {
                error = true;
                $(element).parent().addClass('unset');
            } else {
                $(element).parent().removeClass('unset');
            }
        }

        if ($(element).attr('type') == "checkbox") {
            if ($(element).attr('checked') == false) {
                error = true;
                $(element).parent().addClass('unset');
            } else {
                $(element).parent().removeClass('unset');
            }
        }

        if ($(element).attr('type') == "radio") {
            var radio_error = true;

            //check all having the same name!
            var name = $(element).attr('name');
            $('input[name="' + name + '"]').each(function (index) {
                if (radio_error) {
                    if ($(this).attr('checked') == false) {
                        radio_error = true;
                    } else {
                        radio_error = false;
                    }
                }
            });

            if (radio_error) {
                error = true;
                $(element).parent().addClass('unset');
            } else {
                $(element).parent().removeClass('unset');
            }
        }
    });

    if (!error) {
        $('#form').submit();
    } else {
        $('#error').show();
    }
    //return the valid state here
    return !error;
}

演示:Fiddle

另外,我建议使用onsubmit事件代替onclick事件

<form action="http://www.aol.com" method="post" id="myform" onsubmit="return checkMandatoryFieldsAndSubmit()">
    <input name="carname" type="text" value="" class="mandatory_field" />
    <button type="submit">Submit</button>
</form>

演示:Fiddle