我有一个手风琴,有一个小组,我只想表明是否符合某些标准。面板ID是“product-options”。
我有几个javascript函数,一个是从下拉列表中验证状态(下拉列表id =“省”),另一个是用复选框验证年龄(复选框id =“age_verification”)。手风琴面板“product-options”通过标题激活,id =“BuyThis”。
我想要的是当状态与列表匹配时仅显示面板“product-options”。
这是我的脚本:
<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> 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();
}
答案 0 :(得分:2)
试试这样:
<h3 id="BuyThis" ><button class="Button buyThisBtn"><i class="icon-shopping-cart"></i> 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
请参阅此fiddle并对您的代码进行一些更改:
请注意,您错误地使用$ .inArray方法。在click事件中以及在validateState函数中我认为。
我还对html标记进行了一些更改
编辑3
请参阅此fiddle,并附上一个工作示例: 我希望这可以帮助你。
<强> 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> 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;
}