当前位置: 代码迷 >> JavaScript >> onsubmit validateForm函数无法正常工作
  详细解决方案

onsubmit validateForm函数无法正常工作

热度:56   发布时间:2023-06-13 11:25:27.0

在这里验证,我使用了onsubmit事件。 但任何它不起作用。 我只想检查文本框字段是否为空。

    <!DOCTYPE html>
    <html>
    <head>
        <title>temple3</title>
        <link rel="stylesheet" type="text/css" href="temple3css.css">
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

    </head>
    <body>
        <div id="popup-content" class='container'>
    <div id="container1">
    <form id="form" class="form"  onsubmit="return validateForm()">

    <h1>Feedbak Form</h1>
    <div class="content">
        <div class="intro"></div>
        <div id="section0" >
            <div class="field roption">
            <input type="radio" name="view" value="public"  checked="checked"> <span>Public</span>
            <input type="radio" name="view" value="private" ><span>Private</span>
            </div>
            <div class="field category">
            <label for>Category:</label>
            <select class="dropDownCate" autofocus>
            <option value="bug">Bug</option>
            <option value="1">1</option>
            <option value="2">2</option>
            </select>
            </div>
            <div class="field message">
            <label for="Comments">Comments:</label>
            <textarea id="comments" name="Comments" placeholder='Your Feedback Here!' autofocus></textarea>
            </div>
            <div class="field">
            <label for="Email">Email</label>
            <input type="email" id="email" name="Email" placeholder="example@stevens.edu(optional)" autofocus>
            </div>
            <div class="field"><input type="submit" id="submit-feedback-button"  autofocus></div>
        </div>
    </div>

</form>
 </div>
    </div>
 <div id="popup-overlay-bg"> </div> 
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script>
            function validateForm(){
                var texbox=document.getElementById("comments").value;
                if(texbox == "") {
                    document.getElementById("comments").focus();
                    // document.getElementById("comments").style.border = "1px solid #ff0000";
                    return false;
                }
            }
        $(document).ready(function(){
            $("#feedback-button").click(function(){
               openfeedbackform();
            });
           $("#popup-overlay-bg").click(function(){
               closefeedbackform();
           });
            $("#submit-feedback-button").click(function(){
               // closefeedbackform();
            });
            $(window).resize(function(){
              updatefeedbackform();   
            });
                $("#submit-feedback-button").click(function(){
                 var category=$(".dropDownCate").val();
                 var  roption=$('input:radio[name=view]:checked').val();
                 var message=$("#comments").val();
                 var email=$("#email").val();

                    $.ajax({
                  type: "POST",
                  url: "feedback.php",
                  data: "category="+category+ "&roption="+roption+ "&message="+message+ "&email="+email,

                  success:function(result)
                  {
                    if(result=="true"){
                      $("#form").hide();
                        $("#container1").html("<h3>Thank you for your feedback We will get back to you ASAP</h3> ");
                    }
                    else{
                         $("#form").hide();
                      $("#container1").html("<h3> database error </h3>");
                    }
                  }
                 });
                    return false;
            });

        });
             function openfeedbackform(){
                 $("#popup-content").find('input:text').val('');
                 $("#popup-content").fadeIn();
                 $("#popup-overlay-bg").fadeIn();
                 updatefeedbackform();
             }
            function updatefeedbackform(){
                $popup=$("#popup-content");
                var top = "50px";
                var left = ($(window).width() - $popup.outerWidth()) / 2;
    $popup.css({
        'top' : top,
        'left' : left
    });
            }
            function closefeedbackform(){
                  $("#popup-content").fadeOut();
                $("#popup-overlay-bg").fadeOut();
            } 
        </script>   
    </body>
    </html>

早些时候,我使用相同的表单验证。 但是我不为什么它在这里不起作用。 我试图调试,但尚未调用功能。

问题是您正在按钮单击事件中而不是在表单提交中进行ajax提交。

单击处理程序在提交事件处理程序之前触发,因此验证没有任何效果。

解决方案是使用表单提交事件处理程序进行验证和ajax调用,如下所示,然后就不需要像onsubmit="return validateForm()"这样的内联事件处理程序了。

$("#form").submit(function () {
    if (validateForm() === false) {
        return false;
    }

    var category = $(".dropDownCate").val();
    var roption = $('input:radio[name=view]:checked').val();
    var message = $("#comments").val();
    var email = $("#email").val();

    $.ajax({
        type: "POST",
        url: "feedback.php",
        data: "category=" + category + "&roption=" + roption + "&message=" + message + "&email=" + email,

        success: function (result) {
            if (result == "true") {
                $("#form").hide();
                $("#container1").html("<h3>Thank you for your feedback We will get back to you ASAP</h3> ");
            } else {
                $("#form").hide();
                $("#container1").html("<h3> database error </h3>");
            }
        }
    });
    return false;
});