提交验证

时间:2012-06-05 03:20:40

标签: jquery

我需要帮助提交一份表格,其中我有检查电子邮件存在于下拉列表中,同时提交表格显示提醒信息但也提交表格,当没有显示警告信息时,只有表格需要提交< / p>

$(document).on('click', '#Records', function (event) {
    $("#dropval select option:selected").each(function () {
        var z = $(this).val();
        //alert(z);
        if(z != 'email') {
            alert('please select Email');
            e.preventDefault();
            return false;
        } else {
            return true;
        }
    });
});
});

   <form  name="search" method="post" action="sample1.php" >
  <div id="dropval">

  <select name="select3[1]" class="mySelects" >
  <option value="None"  selected > None</option>
  <option value="email" >email</option>
  <option value="firstname" >firstname</option>
  <option value="lastname" >lastname</option>
  <option value="mobile" >mobile</option>
  </select>
  <select name="select3[2]" class="mySelects" >
  <option value="None" selected > None</option>
  <option value="email" >email</option>
  <option value="firstname" >firstname</option>
  <option value="lastname" >lastname</option>
  <option value="mobile" >mobile</option>
  </select>
  <select name="select3[3]" class="mySelects" >
  <option value="None" selected > None</option>
  <option value="email" >email</option>
  <option value="firstname" >firstname</option>
  <option value="lastname" >lastname</option>
  <option value="mobile" >mobile</option>
  </select>
  <select name="select3[4]" class="mySelect" >
  <option value="None"  selected> None</option>
  <option value="email" >email</option>
  <option value="firstname" >firstname</option>
  <option value="lastname" >lastname</option>
  <option value="mobile" >mobile</option>
  </select>
  </div>
  <input type="submit" value="submit" id="Records">
  </form>

3 个答案:

答案 0 :(得分:0)

如果您在表单提交而不是单击按钮上调用您的函数,则应解决您的问题,因为返回false会停止提交表单。

$('#formName').on('submit', function() {
    //Validation code here
    if(valid == false) {
        return false; //Stops the form from submitting
    } else {
        return true; //Allows the form to submit
    }
});

答案 1 :(得分:0)

$(document).on('click', '#Records', function(event) {
    $("#dropval select option:selected").each( function (){
        var z = $(this).val();
        //alert(z);
        if(z!='email'){
            alert('please select Email');
            event.preventDefault(); // 'e' was undefined before, as you named your variable 'event'. This would have been causing an error and code below would not have been executed - as a result, your form would have been submitted after the alert.
            return false;
        }
     });
});

作为旁注,将事件绑定到document,例如click效率低下。尝试将其绑定到更近的父级。

答案 2 :(得分:0)

试试这个:

$('#formy').bind('submit', function(event){
    var success = false;

    $("#dropval select option:selected").each(function () {
        var z = $(this).val();
        if(z == 'email') {
            success = true;
        }
    });

    return success;
});

你在循环中返回,这对提交没有任何作用。