提交时未清除表单

Form not Cleared on Submit

本文关键字:表单 清除 提交      更新时间:2023-09-26

我在一个网站上工作,在那里我应该在提交时清除注册表。提交时的表单被重定向到一个php脚本,在那里我通过php脚本将表单发送到电子邮件。我不知道我错在哪里,但我无法在提交后清除表格的内容。它成功地检查了空白值,当我提交时,它没有清除控件,我使用的是一个函数clear form,如果我在提交前运行它。。它清除了表单,但没有任何数据发送到脚本。。请帮我进入同一个

这是我的Java scirpt

function verifyEmail(form) {
    //alert(frmContact.email.value);
    checkEmail = contactform.email.value;
    form=contactform;   
    //alert("Please select an answer.");
    if(form.fname.value=='')
        {
            alert("Name is blank");
            form.fname.select();
            return false;
        }
    else if(form.dob.value=='')
        {
            alert("Date Of Birth is blank");
            form.dob.select();
            return false;
        }

        else if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 
    {
    alert("You have entered an invalid email address. Please try again.");
    form.email.select();
    return false;
    } 
        else if(form.mob.value.search(/'d{3}'-'d{3}'-'d{4}/)==-1)
        {
            alert("The phone number you entered is not valid.'r'nPlease enter a phone number with the format xxx-xxx-xxxx");
            form.mob.select();
            return false;
        }
    else if(form.state.value=='')
        {
            alert("State is blank");
            form.state.select();
            return false;
        }
        else if(form.city.value=='')
        {
            alert("City is blank");
            form.city.select();
            return false;
        }

        else if(form.message.value=='')
        {
            alert("Message is blank");
            form.message.select();
            return false;
        }
    else
    {
    form.method="post";
form.target="_self";
form.action="reg_contact.php";
form.submit();
form.fname.value='';
form.dob.value='';
form.email.value='';
form.mob.value='';
form.state.value='';
form.city.value='';
form.message.value='';
clearForm();

    }
    }
    //--></script>
    <script language="javascript">
    function clearForm(form)
    {
        $(":input", form).each(function()
        {
        var type = this.type;
        var tag1= this.tagName.toLowerCase();
        var tag = this.tagName.toLowerCase();
            if (type == 'text' || tag=='textarea' )
            {
            this.value = "";
            }
        });
    };

这是我的表单内容

<form id="contactform" class="form" method="post" name="contactform" onsubmit="return(verifyEmail())" style="height: 499px">
    <fieldset>
    <div>
        <label for="fname" style="width: 216px">Full Name:<span> *</span>
        </label><input id="fname" name="fname" type="text"> </div>
    <div style="margin-left: 45%; margin-top: -13%;">
        <label for="name" style="width: 250px">Date Of Birth:<span> *</span></label><input id="datepicker" name="dob" type="text" />
    </div>
    <div>
        <label style="width: 216px">E-mail:<span>*</span></label><input id="email" name="email" type="text">
    </div>
    <!--
                            <div>
                                <label for="email" >Email: <span>*</span></label>
                                <input name="email" type="email" id="email" pattern="^[A-Za-z0-9](([_'.'-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)((['.'-]?[a-zA-Z0-9]+)*)'.([A-Za-z]{2,})$" />
                            </div>
                    -->
    <div style="margin-left: 45%; margin-top: -13%;">
        <label accesskey="U" for="name" style="width: 216px">Mobile Number:<span> 
        *</span></label> <input id="mob" name="mob" type="text" /> </div>
    <div>
        <label>State:<span> *</span></label>
        <input id="state" name="state" type="text"> </div>
    <div style="margin-left: 45%; margin-top: -12.5%;">
        <label accesskey="U" for="name">City:<span> *</span></label>
        <input id="city" name="city" type="text"> </div>
    <div>
        <label accesskey="U" for="name">Country:<span> *</span></label>
        <input id="country" disabled="disabled" name="country" type="text" value="India" />
    </div>
    <div>
        <label>Course:<span> *</span></label>
        <select id="cource" name="cource" style="height: 34px; width: 241px">
        <option value="Diploma Polytechnic">Diploma - Polytechnic</option>
        <option selected="" value="UG-BE">Bachelor Of Engineering (BE)</option>
        <option value="UG-B.Tech">Bachelor Of Technology (B.Tech)</option>
        <option value="PG-MCA">Master Of Computer Application (MCA)</option>
        <option value="PG-ME">Master Of Engineering (ME)</option>
        <option value="PG-M.Tech">Master Of Technology (M.Tech)</option>
        </select> </div>
    <div>
        <label accesskey="C" for="comments">Message: <span>*</span></label>
        <textarea id="comments" cols="40" name="message" rows="3" spellcheck="true"></textarea>
    </div>
    </fieldset>
    <input id="submit" class="submit" style="height: 36px; width: 90px; margin-top: -0.18%;" type="submit" value="      Send      ">
    <input id="reset" class="reset" style="height: 36px; width: 90px; margin-left: 5px; margin-top: -0.18%;" type="reset" value="   Reset    ">
    <div class="clearfix">
    </div>
</form>

请告诉我哪里出了问题。。。我知道我把这件简单的事搞砸了。

更改

<form ... onsubmit="return(verifyEmail())" ...>

<form ... onsubmit="this.reset(); return verifyEmail();" ...>

或者保留你已经拥有的功能:

function clearForm(form) { // pass a DOM element
    form.reset();
}

为表单<form action="" ... > 提供操作