我想更好地编写我的代码。我不是专业编码员。我想要会员选择我的报价表检查服务,-服务 1,2,3 相同的 grup,-服务 7,8,9,10,11,12,13 另一个 grup,-另一个单 (4,5,6)
我不知道像这段代码这样写得更好更短。太感谢了。
JSFIDDLE
http://jsfiddle.net/elturko/v5rjE/
HTML
<input name="service[]" value="1" class="service_type" type="checkbox"> service 1<br />
<input name="service[]" value="2" class="service_type" type="checkbox"> service 2<br />
<input name="service[]" value="3" class="service_type" type="checkbox"> service 3<br />
<input name="service[]" value="4" class="service_type" type="checkbox"> service 4<br />
<input name="service[]" value="5" class="service_type" type="checkbox"> service 5<br />
<input name="service[]" value="6" class="service_type" type="checkbox"> service 6<br />
<input name="service[]" value="7" class="service_type" type="checkbox"> service 7<br />
<input name="service[]" value="8" class="service_type" type="checkbox"> service 8<br />
<input name="service[]" value="9" class="service_type" type="checkbox"> service 9<br />
<input name="service[]" value="10" class="service_type" type="checkbox"> service 10<br />
<input name="service[]" value="11" class="service_type" type="checkbox"> service 11<br />
<input name="service[]" value="12" class="service_type" type="checkbox"> service 12<br />
<input name="service[]" value="13" class="service_type" type="checkbox"> service 13<br /><br /><br />
<div id="service123">service1 or service2 or service3</div>
<div id="service4">service4</div>
<div id="service5">service5</div>
<div id="service6">service6</div>
<div id="others">other services(7-13)</div>
CSS
div{ display:none;}
JAVASCRIPT
var checked = [];
$("input:checkbox[name=service[]]").live('click', function (){
var $this = $(this),
h = $this.val();
if ($this.is(':checked')) {
checked.push(parseInt(h));
if(h<=3) $("#service123").show(); // 1,2,3 any one
else if(h==4) $("#service4").show();
else if(h==5) $("#service5").show();
else if(h==6) $("#service6").show();
else $("#others").show(); // 7-13 between
}
else{
checked.splice(checked.indexOf(parseInt(h)),1);
if((h <=3) && ($.inArray(1, checked) == -1 && $.inArray(2, checked) == -1 && $.inArray(3, checked) == -1)) $("#service123").hide();
else if(h==4) $("#service4").hide();
else if(h==5) $("#service5").hide();
else if(h==6) $("#service6").hide();
else if((7>= h <=13) && ($.inArray(7, checked) == -1 && $.inArray(8, checked) == -1 && $.inArray(9, checked) == -1 && $.inArray(10, checked) == -1 && $.inArray(11, checked) == -1 && $.inArray(12, checked) == -1 && $.inArray(13, checked) == -1)) $("#others").hide();
}
});