Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用Ajax时如何停止Bootstrap popover中的表单默认操作_Jquery_Ajax_Twitter Bootstrap - Fatal编程技术网

Jquery 使用Ajax时如何停止Bootstrap popover中的表单默认操作

Jquery 使用Ajax时如何停止Bootstrap popover中的表单默认操作,jquery,ajax,twitter-bootstrap,Jquery,Ajax,Twitter Bootstrap,我能够通过Ajax在Bootstrap popover中成功发布此表单,但是e.preventDefault()似乎没有停止默认表单操作,因为页面会在Ajax发布后重新加载。我想知道这是否与popover中的表单有关 HTML <div class="hidden" id="replydiv"> <form class="col-md-12" role="form" id="reply-form" action="<?php echo htmlentities($_S

我能够通过Ajax在Bootstrap popover中成功发布此表单,但是e.preventDefault()似乎没有停止默认表单操作,因为页面会在Ajax发布后重新加载。我想知道这是否与popover中的表单有关

HTML
<div class="hidden" id="replydiv">  
<form class="col-md-12" role="form" id="reply-form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"  >
<div class="form-group">
  <label for="reply">Reply</label>
  <textarea class="form-control" id="reply" name="replytext" maxlength="500" cols="80" rows="6"></textarea>
</div>
<div class="form-group">
  <input type="text" class="form-control" id="lookup" name="reply-lookup"/>
</div>
<input type="submit" class="btn btn-primary" name="post" id="submit-reply" value="Post" />
<p class="close">x</p>
    </form>
</div>

JQ
//------------------- enable the popover ----------------------------
var popup = {trigger:"click",
    title:"Continue the conversation",
    placement: "bottom",
    content:$('#reply-form').detach(),
    html: "true"};

//-------------------- post form ------------------------------------
$('#reply-form').submit(function(e) {
    e.preventDefault()
    var datastring = $(this).serializeArray();
    datastring.push({name:"post", value:"Post"});

    var request = $.ajax({
        type: "POST",
        url: "forumposts.php",
        data: datastring,
        dataType: "json"});
        request.success(function(data) {
        console.log(data);
         })
});
HTML

解决方案1:

添加一个
返回false在AJAX调用之后,如下所示:

这样做:

$('#reply-form').submit(function(e) {
    e.preventDefault()
    var datastring = $(this).serializeArray();
    datastring.push({name:"post", value:"Post"});

    var request = $.ajax({
        type: "POST",
        url: "forumposts.php",
        data: datastring,
        dataType: "json"});
        request.success(function(data) {
        console.log(data);
         })

    return false;
});

解决方案2:

添加

<button class="btn btn-primary" name="post" id="reply-form"/>

添加了此页面,但页面仍会重新加载。这里面临相同的问题。知道你是怎么解决的吗。
$('#reply-form').click(function()
{
        var datastring = $(this).serializeArray();
        datastring.push({name:"post", value:"Post"});

        var request = $.ajax({
            type: "POST",
            url: "forumposts.php",
            data: datastring,
            dataType: "json"});
            request.success(function(data) {
            console.log(data);
             })
});