2012-03-21 334 views
1

我正在一個網站上工作,我必須顯示交付給用戶提供的郵政編碼的商店。所有商店都有自己的div,並且在搜索框被使用或用戶登錄後,郵政編碼會被髮送到隱藏的輸入。這部分對我來說並不難想象。隱藏輸入中的信息被髮送到商店所在頁面的部分也不是一件難事。我遇到麻煩的部分只顯示相關的div。我希望商店的div包含他們以某種方式提供給他們的郵政編碼(無論是否可見)。由於它們都提供了多個郵政編碼,所以我想知道如何推薦我使用隱藏輸入的值來掃描某個郵政編碼,然後顯示它所在的div。使用隱藏的輸入值來顯示或隱藏元素

也許我的措辭使它比實際看起來更復雜。其基本思路是:

<input type="hidden" id="zip" value="x"> 

<div id="store" style="display:none;">Somewhere inside here are some zip codes, one of them has value x (could also be in another</div> 

<script>Some code to make the display style of this particular div "block;"</script> 

我希望有人可以幫助我,這是一個相當的鬥爭。

非常感謝。和平和很多的愛。一!

+0

換句話說:我想顯示一個包含特定元素的div – user1282947 2012-03-21 09:51:02

回答

0
<input type="hidden" id="zip" value="x"> 

<div CLASS="store" style="display:none;">Somewhere inside here are some zip codes, one of them has value x (could also be in another</div> 

<script> 
// check each store and show them if they make reference to the given zip code. 
$(".store").each(function(){ 
if(this.text().indexOf($("#zip").val()) >= 0) 
    this.css({display: "block"}); 
}); 
</script> 
0

試試這個..

<script type"text/javascript"> 
    $(document).ready(function(){ 
    var temp=$("#store").text(); 
    if(temp.indexOf($("#zip").val())!=-1){ 
    $("#store").show();//show this div;mark as visible 
    } 

    }); 
    </script> 
0

我其實解決了這個問題。這比預期的更容易。

如果將有id爲「郵政編碼」的隱藏輸入的代碼基本包括以下關鍵要素:

Function() { 

var value = $("#zipcode").val(); 

$("div:contains(" + value + ")").css("display", "block"); 

} 

的郵政編碼在div可以在一個隱藏的範圍的情況下,你不希望他們顯示。在IE中,由於某種原因span是可見的,但是你可以通過使用隱藏的div而不是跨度來解決這個問題。