javascript - jQuery function not working on Form Submit - Stack Overflow

I saw from W3C example something like this. An action happens on form submit(an alert). However, when I

I saw from W3C example something like this. An action happens on form submit(an alert). However, when I tried with my own function here, my action doesn't happen (show hidden div). Goal is to show hidden div on form submit using 'GET' request. I am new to javascript/jQuery/ajax, but isn't jQuery supposed make call to server without refreshing page?

not working javascript:

$(document).ready(function(){
    $("form").submit(function showDiv(){
        document.getElementById("showMe").style.display = "block";
    });
});

not working html:

<body>
    <form action="" method="GET">
    <input type="text" name="LastName" value="Mouse"><br>
    <input type="submit" value="Submit">
    </form> 

    <div id="showMe" style="display: none;">
    <p>hey this should be hidden</p>
    </div>
</body>

I saw from W3C example something like this. An action happens on form submit(an alert). However, when I tried with my own function here, my action doesn't happen (show hidden div). Goal is to show hidden div on form submit using 'GET' request. I am new to javascript/jQuery/ajax, but isn't jQuery supposed make call to server without refreshing page?

not working javascript:

$(document).ready(function(){
    $("form").submit(function showDiv(){
        document.getElementById("showMe").style.display = "block";
    });
});

not working html:

<body>
    <form action="" method="GET">
    <input type="text" name="LastName" value="Mouse"><br>
    <input type="submit" value="Submit">
    </form> 

    <div id="showMe" style="display: none;">
    <p>hey this should be hidden</p>
    </div>
</body>
Share Improve this question asked May 10, 2016 at 15:05 KervvvKervvv 8455 gold badges15 silver badges27 bronze badges 2
  • 1 Try adding return false; at the end of the submit event handler. Or event.preventDefault() – Tushar Commented May 10, 2016 at 15:06
  • thank you, the event.preventDefault() displayed the hidden text upon button click. – Kervvv Commented May 10, 2016 at 15:38
Add a ment  | 

4 Answers 4

Reset to default 2

You need to cancel the form submit before executing your code:

$(document).ready(function(){
    $("form").submit(function(e){
        // At this point you'll want to have some type of flag to indicate whether you
        // should allow the form submission to continue, or cancel the event. For example,
        // it could be whether not showMe is visible (I don't know what you're going for).

        e.preventDefault();
        $("#showMe").css("display", "block");

        // Submit form after your code runs (or whenever you want)
        $("form").submit();
    });
});
$(document).ready(function(){
    $("form").submit(function(e){
        e.preventDefault(); // this will prevent the submit
        document.getElementById("showMe").style.display = "block";
    });
});

Actually the jfiddle you posted is not blocking the form, it shows you an alert that block all js execution (browser behavior), if you select ok in the alert, the form goes through

the event.preventDefault() statement means: don't process anything outside this function

Try this:

$("form").submit(function(e){
    e.preventDefault();
    $("#showMe").show();
});

You need at least one form element (button or input), that has type "submit". Then jQuery .submit() or .on('submit',..) will definitely work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208156a4616691.html

相关推荐

  • javascript - jQuery function not working on Form Submit - Stack Overflow

    I saw from W3C example something like this. An action happens on form submit(an alert). However, when I

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信