Html jquerypost刷新页面

Html jquerypost刷新页面,html,jquery,Html,Jquery,我有一些jQuery,它接受文本输入的值并将其放入MySQL数据库。但是,当jQuery运行时,页面会刷新,表单中的变量几乎以GET变量的形式出现在URL中。但是,没有一个变量是GET。理想情况下,我希望页面不刷新 jQuery: $('.commentBox').keypress(function(e) { if(e.which == 13) { if ($.trim($(this).val()) == ""){ $('#nocomment')

我有一些jQuery,它接受文本输入的值并将其放入MySQL数据库。但是,当jQuery运行时,页面会刷新,表单中的变量几乎以GET变量的形式出现在URL中。但是,没有一个变量是GET。理想情况下,我希望页面不刷新

jQuery:

$('.commentBox').keypress(function(e) {

    if(e.which == 13) {
        if ($.trim($(this).val()) == ""){
            $('#nocomment').modal('show');
        }
        else {
            var form = $(this).siblings('.commentForm'); 
            var commentbox = $(this).val();

            $.ajax({
                type: "POST",
                url: "../comment",
                data: form.serialize(),
                success: function(){
                    commentbox.val('');
                    form.siblings('.commentContainer').append(response);
                } 
            });
        }
    }

});
HTML(源自PHP):


";
";

您必须返回false或阻止default,这将阻止表单提交:

$('.commentBox').keypress(function(e)
{
    if(e.which == 13)
    {
        e.preventDefault(); // <-- This will stop the form from submitting.

        if ($.trim($(this).val()) == "")
        {
            $('#nocomment').modal('show');
        }
        else
        {
            var form = $(this).closest('.commentForm');
            var commentbox = $(this).val();
            $.ajax({
                type: "POST",
                url: "../comment",
                data: form.serialize(),
                success: function(){
                    commentbox.val('');
                    form.siblings('.commentContainer').append(response);
                }
            });
        }
    }
});
$('.commentBox')。按键(功能(e)
{
如果(e.which==13)
{

e、 preventDefault();//您需要防止在按下enter键(即通过GET提交表单)时发生默认操作

e.preventDefault();


请不要使用return false来完成此操作。它会破坏事件冒泡和捕获,这会使其他javascript难以与您的元素交互。@joseph我应该将其放置在何处?我在keypress函数之后尝试了此操作,但它不允许我在文本框中键入任何内容。然后我将其移动到
else之后{
我可以输入,但当我按enter键时,什么也没有happened@Nate-。我编辑了我的答案。谢谢。太棒了!除此之外,我们显然在同一页上。@user802370-你不应该用
var form=$(this.closest('.commentForm');
?请查看我对Joseph答案的最后评论
$('.commentBox').keypress(function(e)
{
    if(e.which == 13)
    {
        e.preventDefault(); // <-- This will stop the form from submitting.

        if ($.trim($(this).val()) == "")
        {
            $('#nocomment').modal('show');
        }
        else
        {
            var form = $(this).closest('.commentForm');
            var commentbox = $(this).val();
            $.ajax({
                type: "POST",
                url: "../comment",
                data: form.serialize(),
                success: function(){
                    commentbox.val('');
                    form.siblings('.commentContainer').append(response);
                }
            });
        }
    }
});
$( '.commentBox' ).keypress(function( e ) {

    if( e.which === 13 ) {

        // Prevent the default only when it's the enter key
        e.preventDefault();

        if ( $.trim($(this).val()) === '' ){
            $('#nocomment').modal( 'show' );
        }
        else {
            var form = $( this ).siblings( '.commentForm' ); 
            var commentbox = $( this ).val();

            $.ajax({
                type: "POST",
                url: "../comment",
                data: form.serialize(),
                success: function(){
                    commentbox.val( '' ;
                    form.siblings( '.commentContainer' ).append( response );
                } 
            });
        }
    }

});