0

即使存在验证错误,表单也会提交。出现验证错误时如何禁用提交按钮?这是 .js 文件

,submitFu:function(){
_.validateFu(_.labels)                          
if(!_.form.has('.'+_.invalidCl).length)
    $.ajax({
        type: "POST",
        url:_.mailHandlerURL,
        data:{
            name:_.getValFromLabel($('.name',_.form)),
            email:_.getValFromLabel($('.email',_.form)),
            phone:_.getValFromLabel($('.phone',_.form)),
            //fax:_.getValFromLabel($('.fax',_.form)),
            //state:_.getValFromLabel($('.state',_.form)),
            message:_.getValFromLabel($('.message',_.form)),
            owner_email:_.ownerEmail,
            stripHTML:_.stripHTML
        },
        success: function(){
            _.showFu()
        }
    })          
},
showFu:function(){
_.success.slideDown(function(){
    setTimeout(function(){
        _.success.slideUp()
        _.form.trigger('reset')
    },_.successShow)
})
},

这是表格:

    <form id="contact-form" action="contactTq.php" method="post">
                    <div class="success"> Contact form submitted! <strong>We will be in touch soon.</strong></div>
                    <fieldset>
                        <label class="name">
                            <input type="text" name="name" value="Enter Your Name:" >
                            <span class="error">*This is not a valid name.</span>
                            <span class="empty">*This field is required.</span>
                            <span class="clear"></span>
                        </label>
                        <label class="email">
                            <input type="text" name="email" value="Enter Your E-mail:">
                            <span class="error">*This is not a valid email address.</span>
                            <span class="empty">*This field is required.</span>
                            <span class="clear"></span>
                        </label>
                        <label class="phone">
                            <input type="text" name="phone" value="Enter Your Phone (optional):">
                            <span class="error">*This is not a valid phone number.</span>
                            <span class="clear"></span>
                        </label>
                        <label class="message">
                            <textarea name="msg">Enter Your Message:</textarea>
                            <span class="error">*The message is too short.</span>
                            <span class="empty">*This field is required.</span>
                            <span class="clear"></span>
                        </label>
                        <div class="buttons"><strong><a class="button" data-type="reset">Reset<span></span></a></strong><strong><a class="button" data-type="submit" href="javascript:{}" onclick="document.getElementById('contact-form').submit();">Submit<span></span></a></strong></div>
                    </fieldset>
                </form>
4

3 回答 3

0

Knockout.js 完美地完成了您正在寻找的工作。您可以将现有的 javascript 放入 ViewModel,然后在您的按钮上放置一个数据绑定以启用所有字段是否都有效。您可以在此处查看如何操作:http: //knockoutjs.com/documentation/enable-binding.html

于 2013-03-29T16:29:52.013 回答
0

您似乎在单击按钮后立即提交表单,因为 onclick 处理程序编写如下:

onclick="document.getElementById('contact-form').submit();"

只需更改 onclick 处理程序以执行该submitFu功能,这应该可以解决问题。

于 2013-03-29T16:40:25.523 回答
0

将提交按钮的 onclick 事件更改为 onclick="return submitFu()"

如果验证失败,则返回 false 并且表单将不会提交。我认为您可以对表单的 onsubmit 事件执行相同的操作。

于 2013-03-30T02:36:27.827 回答