开发者

using jquery validate but prevent the form submit

the form is still submitted. if i want to use your code and add a step.that is if show the red text then preventing the form to submit?how do i do? thank you.

Here is my script:

jQuery(function($)
{
    if (!$("#edit-name").val() || !$("#edit-email").val() || !$("#edit-comment-body").val())
    {
        $("#edit-submit").click(function()
        {
            $('#edit-name').after('<span style="color:red;">请输入你的名字</span>');
            $('#edit-mail').after('<span开发者_如何转开发 style="color:red;">请输入你的邮箱</span>');
            $('#edit-comment-body label span').append('<span style="color:red;">请填写内容</span>');
        });
    }
});


validate the fields inside submit event like:

$('#formID').submit(function(){
      if ($("#edit-name").val()=='' || $("#edit-email").val()=='' || $("#edit-comment-body").val()==''){
        $('#edit-name').after('<span style="color:red;">è¯·è¾“å…¥ä½ çš„åå­—</span>');
        $('#edit-mail').after('<span style="color:red;">è¯·è¾“å…¥ä½ çš„é‚®ç®±</span>');
        $('#edit-comment-body label span').append('<span style="color:red;">请填写内容</span>');

        return false; // to stop form submission in any way

} });


on the .click(function()) give it a parameter e

    $("#edit-submit").click(function(e)
    {
        e.preventDefault();
        $('#edit-name').after('<span style="color:red;">请输入你的名字</span>');
        $('#edit-mail').after('<span style="color:red;">请输入你的邮箱</span>');
        $('#edit-comment-body label span').append('<span style="color:red;">请填写内容</span>');
    });


Normally, you hook an event listener to the submit event of the form, from there you can return false.

$("form#foo").submit(function(){
    // validate
    return false;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜