2012-09-09 131 views
0

我可以使用jquery顯示帶有以下代碼的鏈接標籤的表格。單擊按鈕的隱藏表

$(document).ready(function() 
{ 
    $("#show").click(function() 
    { 
     $("#table").show(); 
    }); 
}); 

但是當我想爲一個按鈕,爲此,它會顯示錶第二那麼它會被隱藏。這些按鈕的代碼:

$(document).ready(function() 
{ 
    $("#button").click(function() 
    { 
     $("#table").show(); 
    }); 
}); 

更新:

$(document).ready(function() 
{ 
    $("#table").hide(); 
    $("#button").click(function() 
    { 
     $("#table").show(); 
    }); 

}); 

下面是代碼:

<script type="text/javascript" src="jquery-1.8.1.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() 
{ 
    $("#table").hide(); 
    $("#button").click(function() 
    { 
     $("#table").show(); 
    }); 

}); 
</script> 
</head> 

<body> 
<p><a href="#" id="show">Show</a> <a href="#" id="hide">Hide</a></p> 
<form id="form1" name="form1" method="post" action=""> 
    <p> 
    <input type="submit" name="button" id="button" value="Show" /> 
    </p> 
    <p>&nbsp; </p> 
</form> 
<table width="515" border="0" class="table1" id="table"> 
    <tr> 
    <td width="509" class="table1"><img src="Image/Tulips.jpg" width="400" height="400" alt="Tulips" /></td> 
    </tr> 
</table> 
+0

請演示,例如http://jsfiddle.net。 –

+1

適用於我: http://jsfiddle.net/hmartiro/RMxq4/ –

+0

發表了更多你的代碼,還有一些事情是你沒有給我們看的 – aquinas

回答

3

你的代碼工作得很好,也許你換貨什麼是toggle在這裏你去:Demo

我只是改變.show().toggle()

$(document).ready(function() 
{ 
    $("#button").click(function() 
    { 
     $("#table").toggle(); 
    }); 
}); 

UPDATE: 的problam是您在使用type="submit"在您的按鈕上,導致表單被提交...更改爲type="button"

另一種方式(即保持type="submit"有):

$(document).ready(function() 
{ 
    $("#table").hide(); 
    $("#button").click(function() 
    { 
     $("#table").show(); 
     return false;   
    }); 

}); 

我加return false;防止按鈕默認操作。

+0

只是顯示一秒,那麼它將被隱藏 – aliboy38

+0

比你的代碼中必須有一個problam,因爲這部分是好的 - 向我們展示你的所有代碼。 –

+0

看到代碼..... – aliboy38

1
$(document).ready(function() 
{ 
    $("#button").click(function() 
    { 
     $("#table:hidden").show(); 
     $("#table:visible").hide(); 
    }); 
}); 

或者:

$(document).ready(function() 
{ 
    $("#button").click(function() 
    { 
     $("#table").toggle(); 
    }); 
}); 

編輯作爲您的評論規定:

$(document).ready(function() 
{ 
    $("#button").click(function() 
    { 
     $("#table").show(); 
     setTimeout(function() 
     { 
      $("#table").hide(); 
     }, 2000); 
    }); 
}); 

表將出現2000毫秒(2秒)和隱藏其後。

+0

只顯示一秒就會隱藏 – aliboy38

+0

@ aliboy38更新到規範在上面的評論 – esqew

+0

我認爲@ aliboy38描述在這個評論之上這是一個不需要的行爲,而不是如何得到它。就是說:'#按鈕'是表單標籤的提交。因此,「它顯示一秒鐘,然後再次隱藏」。因爲該頁面正在重新加載。 –

0

只需在這裏使用這些if語句。

0「>然後你的表標籤去這裏 我們可以使用上面的JavaScript,但是,如果提交按鈕有一些行動來做它不會知道要調用哪個動作..要隱藏表或執行提交按鈕後給出的任務,所以使用上面的if語句,它對我來說就像一個魅力!:)