2013-05-11 70 views
0

這是如此奇怪,這段代碼不工作..我只想要一個div隱藏,當它加載和顯示時,我點擊新建按鈕..請告訴我在哪裏做錯了..謝謝!簡單jquery顯示div不工作

<?php 
include('connect.php'); 
?> 

<html> 
<head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script> 

<script> 



    $(document).ready(function(){ 


$('#form2').hide(); 

    $("#form1").validate({ 
     debug: false, 
    rules: { 

    plid:"required", 
     }, 
    messages: { 

    plid: "Please select a pack name id..", 
    }, 

    submitHandler: function(form) { 

    $.ajax 
    ({ 




type: "POST", 
url: "aa.php", 
data: $('#form1').serialize(), 
cache: false, 

success: function(response) { 
    $('#result1').html(response); 

     } 
     }); 
     } 

    }); 
    }); 


    </script> 
    <script> 
$("#new").click(function(){ 

    $("#form2").show(); 
}) 
    </script> 

</head> 
<body> 

Packing List 
</br> 
<form id="form1" name="form1" action="" method="post"> 
<?php 


echo '<select name="plid" id="plid">'; 
echo '<option value="" selected="selected">--Select the Pack Name--</option>'; 
$tempholder = array(); 
$sql="SELECT CONCAT(p.pl_no, '_', DATE_FORMAT(p.pl_dt, '%d/%m/%Y') , '_', g.acname) AS plname, p.pl_id FROM packlist p, glmast g WHERE g.gl_id = p.gl_id ORDER BY pl_dt DESC , pl_no DESC LIMIT 30"; 


$query = mysql_query($sql) or die(mysql_error()); 
$nr = mysql_num_rows($query); 
for ($i=0; $i<$nr; $i++){ 
$r = mysql_fetch_array($query); 
if (!in_array($r['plname'], $tempholder)){ 
$tempholder[$i] = $r['plname']; 
echo "<option value='$r[pl_id]'>".$r["plname"]."</option>"; 
} 
} 

echo '</select>'; 

?><br/> 

<input type="submit" name="delete" value="Delete"/><br/> 
<input type="submit" name="edit" id="edit" value="Edit"/><br/> 

</form> 
<form> 
<input type="submit" name="new" id="new" class="new" value="New" /><br/> 
</form> 

<div id="newform" class="newform"> 

     <form name="form2" class="form2" id="form2" method="post" action="aa.php"> 
     <P> 
      <LABEL for="plidnew">PackList No 
         <INPUT type="text" id="plidnew" name="plidnew"></LABEL><BR><BR> 
      <LABEL for="itemidnew">Item Id 
         <INPUT type="text" id="itemidnew" name="itemidnew"></LABEL><BR><BR> 
      <LABEL for="quannew">Quantity 
         <INPUT type="text" id="quannew" name="quannew"></LABEL><BR><BR> 
      <LABEL for="potnew">Potency 
         <INPUT type="text" id="potnew" name="potnew"></LABEL><BR><BR> 
      <LABEL for="sizenew">Size 
         <INPUT type="text" id="sizenew" name="sizenew"></LABEL><BR><BR> 

      <INPUT type="submit" id="newsubmit" name="newsubmit" value="Submit"> <INPUT type="reset"> 
      </P> 

     </form> 

</div> 
<div id="result1"></div> 
</body> 

</html> 

於是,我的jQuery的各種組合,但是當頁面加載的div隱藏..但是,當我點擊它沒有顯示up..Any幫助!

+0

在創建完所有DOM元素後,在頁面末尾放置'$(document).ready(function(){...});'函數。 – jap1968 2013-05-11 11:03:21

+0

請添加JF小提琴 – 2013-05-11 11:03:47

+0

@ jap1968我試過..沒有影響..謝謝你的答覆.. :) – user2234992 2013-05-11 11:07:53

回答

1

你應該把所有的代碼更具體的代碼,而不是隻複製粘貼&所有的代碼,因爲我們不能試試你的腳本,就像連接到MySQL ...但

你應該把

$("#new").click(function(){ 
     $("#form2").show(); 
}); 

$(document).ready(function(){... 

,而不是使用類型=「提交」的所有按鍵,因爲它推出形式提交,你應該例如使用:

<input type="button" name="new" id="new" class="new" value="New" /> 
+0

感謝您的答案..我把代碼內準備好的功能..它的工作..但後來顯示div,加載頁面,表單消失...任何想法什麼是錯的..謝謝... – user2234992 2013-05-11 11:18:15

+0

這是因爲輸入類型=「提交」,啓動頁面的重新加載,使用輸入類型=「按鈕」 – carlituxman 2013-05-11 11:25:15