0

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>');
        });
    }
});

3 Answers 3

2

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

} });

Sign up to request clarification or add additional context in comments.

1 Comment

here still have a question.after i click the button, if i click again. the red text will add once again. how to prevent it. thank you.
1

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>');
    });

3 Comments

many thanks. why i using submit()function. the js code can't work. there still have a question.after i click the button, if i click again. the red text will add once again. how to prevent it. thank you.
what's the parameter e meaning?
e is the event parameter. Can you elaborate on your other question a bit? I don't quite follow.
1

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;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.