1

所以我已经完成了所有工作,我收集了用户的邮政编码,根据覆盖区域中的一系列邮政编码对其进行测试,如果用户在服务区域,则显示购物车和愿望清单,如果他们没有禁用购物车按钮并仅允许愿望清单。我知道如何做到这一点,但不知道如何让它留在会话中,例如:如果用户转到不同的页面,我需要继续禁用购物车。我知道我需要使用 cookie 或其他东西,我觉得我很接近了。任何一点点都有帮助。谢谢...

    <script>
    var zipCodeArray = ["98001",
    "98002", "98003", "98004", "98005", "98006"]
     $("#zipCode").live('keyup', function(){
       var zipCode = $(this).val();
       if(zipCode.length >4){
         if($.inArray(zipCode, zipCodeArray) > -1){
          //display the hidden elements if zip is in list 
          $("#cart").css("display:block");
         }else{
           alert("We only service this area...but you can add to wish list         ");
           $("#button-cart","#or").css("display:none");
           //hide add to cart and the "- OR -" text.
         } 
       }
    });    
    </script>
    </head>
    <body>
    //make this appear in colorbox
    <div class="zipCode">
    <form>
    <input id="zipCode" name="zipCode" />
    <input type="submit" value="Submit">
    </form>
    </div>
    </body>
    </html>
4

1 回答 1

1

自 jQuery 1.7 起,我使用了onsincelive已弃用,我更喜欢听模糊的性能。

演示

 $(window).load(function () {
var zipCodeArray = ["98001", "98002", "98003", "98004", "98005", "98006"];
//Get user cookie match zip and 5 digits only. Assign False if it doesn't
var userZip = document.cookie.match(/zip=(\d{5})/) || false;
//Cache #cart
var cart = $("#cart");
//Cache #zipCode
var input = $("#zipCode");
if (userZip) {
    input.val(userZip[1]);//Inject cookie value to input
    cart.show();//Show cart
}
input.on('blur', function () {
    //If userZip is defined use userZip
    var zipCode = userZip ? userZip[1] : $(this).val();
    console.log(zipCode);//Debugging
    //No need to check for input length
    if ($.inArray(zipCode, zipCodeArray) > -1) {
        console.log('in array');
        //display the hidden elements if zip is in list 
        cart.show();
        if (!userZip) {
            document.cookie = "zip=" + zipCode;//Save cookie
            console.log("cookie " + document.cookie);//Debugging
        }
    } else {
        alert("We only service this area...but you can add to wish list");
        $("#button-cart", "#or").css("display:none");
        //hide add to cart and the "- OR -" text.
    }
});
})
于 2013-07-13T00:38:45.723 回答