简体   繁体   English

使用javascript取消表单提交无法正常工作

[英]Form submission cancel using javascript not working

OBJECTIVE: I have made a form containing 3 fields, I want that If there's even a single empty field left in form the form , it is recognised and an error message(for each empty field) is printed (not alerted), and form submission is cancelled. 目标:我已经制作了一个包含3个字段的表单,我希望如果表单中只剩下一个空字段,它会被识别并打印一条错误消息(每个空字段)(不提醒),然后提交表单取消。 The error msg is initially hidden and it should be made visible only when a field is left empty. 错误msg最初是隐藏的,并且仅当字段为空时才应使其可见。

PROBLEM: the code to stop submission is not working. 问题:停止提交的代码不起作用。 Please post any error. 请发布任何错误。 It works if use only alert(), and remove the code to show and delete hidden validaeFormOnSubmit() and validateEmpty(). 如果仅使用alert(),它可以工作,并删除代码以显示和删除隐藏的validaeFormOnSubmit()和validateEmpty()。

FORM: 形成:

<form action="start.php" method="post" onsubmit="return validateFormOnSubmit(this)" >
<table>
  <tbody>
  <tr>
     <td><label for="fname">Team Name:</label><br></td>
    <td><input name="fname"  type="text" autocomplete="off">&nbsp <div id="1"> Field is 
required 
</div></td>
  </tr>   
  <tr>
 <td><label for="contact">Contact 1 :</label> </td>
 <td><input type="text"  maxlength = "10" name="contact" > &nbsp<div id="2"> Field 
 is required </div></td>
  </tr>   
  <tr>
    <td><label for="contact2">Contact 2:</label> </td>
    <td><input type="text"  maxlength = "10" name="contact2" >&nbsp <div id="3"> Field is     
    required </div></td>
  </tr>  
  <tr>
    <td>&nbsp;</td>
    <td><input name="Submit" value="Send" type="submit" ></td>
    <td>&nbsp;</td>
  </tr>
  </tbody>
</table>
</form>

SCRIPT 脚本

<script >
$(document).ready(function () {
            var i;
            for(i=1;i<=3;i++)
                {
                var k='#'+i;
                $(k).hide();}

            });

function validateFormOnSubmit(theForm) {
    var reason = "";

   reason += validateEmpty(theForm.fname,1);
   reason += validateEmpty(theForm.contact,2);
   reason += validateEmpty(theForm.contact2,3);

  if (reason != "") {

    return false;
  }
  return true;
  }

  function validateEmpty(fld,k) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "error";
        var k='#'+i;
        $(k).show();
    } else {
        fld.style.background = 'White';
        var k='#'+i;
            $(k).hide();
        }
        return error;  
    }
    </script>

I would suggest the following: 我建议以下内容:

  • Remove the onsubmit="..." from your HTML 从您的HTML中删除onsubmit="..."
  • Bind to the onsubmit event programmatically: 以编程方式绑定到onsubmit事件:

     $(...).on("submit", validateFormOnSubmit); 
  • Change the signature of validateFormOnSubmit accordingly: 相应地更改validateFormOnSubmit的签名:

     function validateFormOnSubmit(event) { // use this instead of theForm } 
  • Instead of returning false do event.preventDefault() , ie: 而不是返回false来做event.preventDefault() ,即:

     if (reason != "") { event.preventDefault(); } 

What about this ? 那这个呢 ? http://jsfiddle.net/45Kfy/ http://jsfiddle.net/45Kfy/

<code>
  $(document).ready(function () {
               $("input").each(function(index, item)
               {
                   if ($(item).val().length == 0)
                   {
                      $(item).css("background-color", "red");
                   }
               });
           });
 </code>

Instead of documentReady() you should trigger the button click. 代替documentReady(),您应该触发按钮单击。 ( jQuery get the input value in an .each loop ) And watch out, your button is an inputfield too. jQuery在.each循环中获取输入值 )并且要注意,您的按钮也是输入字段。

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

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