weixin_33714884 2016-10-16 16:41 采纳率: 0%
浏览 15

jQuery-表单未验证

In the following form when I hit submit, it alerts 111, but does not send an ajax request. It instead sends a page-refreshing submit request. Why?

<form id="post_answer_form" method="POST" action="some_url">
    <fieldset>
        <textarea id="qanswer" name="qanswer"></textarea>
        <button type="submit">Post Your Answer </button>
        <button type="submit" style="display: none;">Update </button>
    </fieldset>
</form>

I'm validating it against some rules as follows:

$(document).on('submit', '#post_answer_form', function(){
    alert(111)
    tinymce.triggerSave();
}).validate({
    ignore: '',
    rules: {
        qanswer: {
            required: true,
        }
    },
    submitHandler: function(form) {
        alert(111)
        $.ajax({
            ...
        });
        return false
    },
});
  • 写回答

1条回答 默认 最新

  • weixin_33717117 2016-10-16 17:44
    关注

    Your validation is not working because you are not applying validate on form . you can try following code.

    $(function(){   
        $('#post_answer_form').submit(function(e) {
            e.preventDefault();    
            tinymce.triggerSave();
            if($('#post_answer_form').valid()){
                // Submit form using ajax or using .submit() 
                $.ajax({
                    ...
                });
            }
        }); 
    
        $('#post_answer_form').validate({
            ignore: '',
            onsubmit : false,
            rules: {
                qanswer: {
                    required: true,
                }
            }
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀