0

我有一个带有一个面板的手风琴,我只想在满足某些标准时展示。面板 ID 是“产品选项”。

我有几个 javascript 函数,一个是从下拉列表中验证状态(下拉列表 id="province"),另一个是使用复选框验证年龄(复选框 id="age_verification")。手风琴面板“product-options”通过标题 id="BuyThis" 激活。

我想要的是仅在状态与列表匹配时显示面板“产品选项”。

这是我的脚本:

<script>
// Validates State is in list of allowed for wine shipping...  
function validateState() {
      // Confirms province is allowed for wine shipping     
          var states = ["Alaska", "Arizona", "California", "Colorado", ""];
                 if ($.inArray($("#province").val(), states) <0) {
                 alert("Shipping gifts containing alcohol to this state is prohibited by law. Please choose another item.");
                 return false;       
        }
alert("Item Available! Please choose your options.");
return true;
}
    // Validates age verification is checked...   
    function validateAge() {    
        if (!$('#age_verification').is(':checked')) {
            alert("Please verify you are 21 years of age or older.");
            return false;       
        }    
return true;
}
</script>

对应的HTML:

<h3 id="BuyThis" onclick="return validateState();"><button class="Button buyThisBtn"><i class="icon-shopping-cart"></i>&nbsp;&nbsp;Buy This!</button></h3>              
<div class="ProductContainer" id="product-options"> 
    <form>
    [BODY OF ACCORDION PANEL WITH A BUNCH OF IRRELEVANT CODE......]     

             <button id="addtocart" type="submit" class="Button" onclick="validateAge(); return validateState();"><i class="icon-shopping-cart"></i> ADD TO CART</button>                   
    </form>
</div>

我尝试合并以下内容无济于事:

$(function () {
    $("#buyThis").change(function () {
    if ($.inArray($("#province").val(), states) <0) {                
         alert("Shipping gifts containing alcohol to this state is prohibited by law. Please choose another item.");
         $("product-options").hide();               
    }                
$("product-options").show();             
}
4

1 回答 1

2

试试这样:

<h3 id="BuyThis" ><button class="Button buyThisBtn"><i class="icon-shopping-cart"></i>&nbsp;&nbsp;Buy This!</button></h3>              
<div class="ProductContainer" id="product-options"> 
    <form>
    [BODY OF ACCORDION PANEL WITH A BUNCH OF IRRELEVANT CODE......]     

             <button id="addtocart" type="submit" class="Button" onclick="validateAge(); return validateState();"><i class="icon-shopping-cart"></i> ADD TO CART</button>                   
    </form>
</div>


$(function () {
    $("#buyThis").click(function () {

if(validateState()){

       if ($.inArray($("#province").val(), states) <0) {                
         alert("Shipping gifts containing alcohol to this state is prohibited by law.         Please choose another item.");
         $("product-options").hide();               
       }                
    $("product-options").show();
  }             
}

编辑 2

请参阅此小提琴,对您的代码进行一些更改:

请不要以错误的方式使用 $.inArray 方法。在单击事件中以及我认为的 validateState 函数中。

我还对 html 标记进行了一些更改

编辑 3

请看这个带有工作示例的小提琴:我希望这现在可以帮助你。

html

<select id="province">
    <option value="Alabama">Alabama</option>
    <option value="Alaska">Alaska</option>
    <option value="Arizona">Arizona</option>
    <option value="Arkansas">Arkansas</option>
    <option value="California">Colorado</option>
</select>
         <button id="test" >Teste</button>  
<h3 id="BuyThis" style="background:red;" >

    <button id="buyThisButton" class="Button buyThisBtn"><i class="icon-shopping-cart"></i>&nbsp;&nbsp;Buy This!</button>
</h3>              
<div class="ProductContainer" id="product-options"> 
    <form>
    [BODY OF ACCORDION PANEL WITH A BUNCH OF IRRELEVANT CODE......]    
        <button id="addtocart" type="submit" class="Button" onclick="validateAge(); return validateState();"><i class="icon-shopping-cart"></i> ADD TO CART</button>                   
    </form>
</div>

javascript

$( document ).ready(function() {
  // Handler for .ready() called.

    $("#BuyThis").click(function() {
    //alert("test");

     if(validateState()){
         // if state is valid do this

     }else{
     // do that

     }
  });


}); // end of dom ready



// Validates State is in list of allowed for wine shipping...  
function validateState() {
      // Confirms province is allowed for wine shipping     
          var states = ["Alaska", "Arizona"];
                 if ($.inArray($("#province").val(), states) >= 0) {                     
                 alert("Oh no! Shipping items containing alcohol to this state is prohibited by law. Please choose another item.");
                 //$("#BuyThis").unbind("click");
                 $("#product-options").slideUp(); 
                 return false;       
        }
    alert("Item Available! Please choose your options.");
    $("#product-options").slideDown();  
    return true;
}


    // Validates age verification is checked...   
    function validateAge() {    
        if (!$('#age_verification').is(':checked')) {
            alert("Please verify you are 21 years of age or older.");
            return false;       
        }    
return true;
}
于 2013-11-14T00:05:23.140 回答