我想更好地寫我的代碼。我不是專業編碼器。 我希望會員選擇我的報價表格檢查服務, - 服務1,2,3相同grup, - 服務7,8,9,10,11,12,13另一個grup, - 和另一單(4,5 ,6)我怎樣才能寫得更好我的jQuery代碼?
我不知道如何更好和短的寫這樣的代碼。非常感謝。
的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();
}
});
http://codereview.stackexchange.com/ –