2014-06-26 46 views
0

我用PHP和JQuery做了一點警告/警告框,而且我遇到了一些問題。我的問題是,當我點擊關閉時,該框不會保持關閉狀態,並且不會關閉正確的框。如果有多個警報,我希望箱子堆疊起來,並且彼此重疊,所以當您關閉最上面的那個時,就會顯示出來。在關閉它的時候,我試圖關閉的那個盒子關閉,它只關閉了大約3秒(然後頁面被JQuery刷新並再次打開。)所以我需要一種方法來刪除盒子,但我不能沒有得到盒子的ID。這是我正在使用的代碼。PHP和JQuery刪除正確的東西

<script> 

function open_box() 
{ 

    $("#box").show(); 

} 

$(document).ready(function() 
{ 

    $(".close").click(function() 
    { 


     $("#box").fadeOut("slow"); 
     //$("body").load("update_box"); 

    }); 

    $("#box").dblclick(function() 
    { 

     $("#box").effect("highlight"); 

    }); 

}); 

</script> 
<style> 
     .AlertTitle 
     { 

      font-size:2em; 
      text-align:center; 
      position:relative; 
      top:-8px; 

     } 

     #box 
     { 

      background-color:#fefefe; 
      border:2px solid #5556a7; 
      border-radius:10px; 
      width:50%; 
      height:20%; 
      position:fixed; 
      z-index:1000; 
      left:25%; 
      padding-left:25px; 
      padding-top:10px; 
      top:30%; 
      box-shadow:0px 0px 100px 2px grey; 
      overflow-x:hidden; 

     } 

     .close 
     { 

      position:absolute; 
      top:-2px; 
      left:-1px; 
      border:2px solid #5556a7; 
      border-bottom-right-radius:10px; 
      border-top-left-radius:10px; 
      width:15px; 
      height:19px; 
      padding-top:2px; 
      padding-left:2px; 

     } 

     .close:hover 
     { 

      font-weight:bold; 

      cursor:pointer; 

     } 

    </style> 
<?php 

require("core.php"); 
$id = $user->user_id(); 
$query = mysql_query("SELECT * FROM `warnings` WHERE `user_id` = '$id' ORDER BY `id` ASC") or die(mysql_error()); 
while($row = mysql_fetch_assoc($query)) 
{ 

    ?> 

    <script> 

     open_box(); 

    </script> 
    <div id="box"><?php echo $row["content"]; ?></div> 
    <div class="close">X</div> 
    <?php 

} 

?> 
+1

[**請不要在新代碼中使用'mysql_ *'函數**](http://stackoverflow.com/q/12859942)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://uk.php.net/manual/en/function.mysql-connect.php)?學習[*準備的語句*](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli) - [這篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)將幫助你決定哪個。 –

回答

0

不能使用同一個ID爲一個以上的元素

<div id="box"><?php echo $row["content"]; ?></div>是罪魁禍首

使用

<div class="box"><?php echo $row["content"]; ?></div> 
    <div class="close">X</div> 

改文風指爲.box的,而不是#box

支票this fiddle

+0

我不知道我怎麼能改變這一點,我用我的風格id「盒子」。 – user3689853

+0

使用class =「盒子」,並與它的樣式 – dev

+0

也tou必須改變jquery腳本來引用從關閉按鈕,而不是固定的#box或.box – dev