2016-03-15 39 views
0

我已經把一個模態彈出框用於獲取輸入,然後通過ajax存儲。它插入值成功..但提交後彈出靜止。我已經嘗試了一切像window.close(); ...('#modalname')。hide();但沒有爲我工作。請幫我解決這個錯誤。提交後jQuery openPopup模式沒有關閉

這是我的工作代碼的一部分,

     <!--css for display subject in row (starts)--> 
         <span class="b-messages__subject"> 
          <span> 
           <a href="index.php?subject=<?php echo $results[$result]['subject']; ?>&username=<?php echo $results[$result]['username']; ?>#openModal" onclick="fetch_select(<?php echo $results[$result]["id"]; ?>);"> 
            <?php echo "Reply"; ?> 
           </a> 
          </span> 
         </span> 
         <!--css for display subject in row (ends)--> 

,然後模態DIV是,

     <!--Modal box starts--> 
         <div id="openModal" class="modalDialog" align="center"> 
          <div> 
           <a href="<?php echo ('/'); ?>yandex" title="Close" class="close">X</a> 
           <h2><strong>Reply Message</strong></h2> 
           <br> 
           <label><strong>Enter Your Message Here</strong></label> 
           <br> 
           <label><b>From:</b>&nbsp;&nbsp;<?php echo $user; ?></label> 
           <span id="content-info" class="info"></span> 
           <br/> 
           <!--set session username for hidden--> 
           <input type="hidden" name="username" id="username" value="<?php echo $user; ?>"> 
           <textarea name="content" id="content" class="demoInputBox"></textarea> 
           <!--send fusername with hidden--> 
           <input type="text" name="fusername" id="fusername" value="<?php echo $_GET["username"] ?>"> 
           <!--send subject with hidden--> 
           <input type="text" name="subject" id="subject" value="<?php echo $_GET["subject"] ?>"> 
           <!--status init="0"--> 
           <input type="hidden" name="status" id="status" value="0"> 
           <!--time--> 
           <input type="hidden" name="created" id="created" value='<?php echo date("Y-m-d H:i:s"); ?>'> 
           <br><br> 
           <input type="button" name="submit" id="but-sub" value="Send Message" onClick="add();" /> 
          </div> 
         </div> 
         <!--Modal box ends--> 

我的js文件包含:

function add() { 
     /*initialize valid and assign to function validate()*/ 
     var valid = validate(); 
     //alert(valid); returns true 
     //if function validate() returns valid.. then go away 
     if (valid){ 
      $.ajax({ 
       url: "add.php", 
       type: "POST", 
       data: { 
        username: $("#username").val(), 
        fusername: $("#fusername").val(), 
        subject: $("#subject").val(), 
        content: $("#content").val(), 
        status: $("#status").val(), 
        created: $("#created").val()} 
      }); 
     } 
    } 
+0

你使用jQueryUI嗎? – dgig

回答

0

我不確定您是否使用JSON,但您一定要檢查通過或失敗的響應。嘗試添加成功後,你的數據是這樣的(不包括檢查通過或失敗,但應在AJAX的成功響應時關閉模式):

$.ajax({ 
    url: "add.php", 
    type: "POST", 
    data: { 
     username: $("#username").val(), 
     fusername: $("#fusername").val(), 
     subject: $("#subject").val(), 
     content: $("#content").val(), 
     status: $("#status").val(), 
     created: $("#created").val() 
    }, 
    success: function (returndata) { 
     //try this first 
     $("#openModal").modal('hide'); 

     //try this second and uncomment if you aren't using jquery/bootstrap modal 
     //$("#openModal").hide(); 

    } 
}); 
0

把你的表單元素在<form></form> 。 模態應該關閉,如果形式submited。