2012-03-21 197 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而不是跨度来解决这个问题。