0
想知道是否有人可以幫助我改進我的代碼。它只是一些基本的複選框選擇,但我知道必須有一些簡短的做事方式。改進這個jQuery?
jQuery的:
$(document).ready(function() {
$(".sendType").click(function() {
var what_is = $(this).attr("name");
var p = new RegExp('.*?(\\d+).*?((?:[a-z][a-z]+))',["i"]);
var m = p.exec(what_is);
if (m != null) {
check_all(m[1], m[2], (this).checked);
}
});
$(".controlType").click(function() {
var what_is = $(this).attr("name");
check_all(0, what_is, (this).checked);
});
function check_all(id, what, check) {
all = $("input:checkbox");
sa = $("input:checkbox.sendType."+id);
sac = $("input:checkbox:checked.sendType."+id).length;
sw = $("input:checkbox.sendType."+what);
if (id == 0) {
if (what == 'all') {
all.attr('checked', check);
} else {
sw.attr('checked', check);
}
}
if (what == 'all') {
sa.attr('checked', check);
} else {
if (sac == 2) {
if (check === true) {
sa.attr('checked', true);
} else {
$("input:checkbox.sendType.all."+id).attr('checked', false);
}
}
}
ce = $("input:checkbox.controlType.email");
ct = $("input:checkbox.controlType.text");
s2 = $("input:checkbox:not(:checked).sendType.email").length;
s3 = $("input:checkbox:not(:checked).sendType.text").length;
if (!s2) {
ce.attr('checked', true);
} else {
ce.attr('checked', false);
}
if (!s3) {
ct.attr('checked', true);
} else {
ct.attr('checked', false);
}
if (!s3 && !s2) {
all.attr('checked', true);
} else {
$("input:checkbox.controlType.all").attr('checked', false);
}
}
});
的標記:
<ul class="form-section">
<li class="form-line">
<input type="checkbox" class="controlType all" name="all" value="1">
<input type="checkbox" class="controlType email" name="email" value="1">
<input type="checkbox" class="controlType text" name="text" value="1">
</li>
<li class="form-line">
<input type="checkbox" class="sendType all 1" name="id[1][all]" value="Charlie Gery allen">
<input type="checkbox" class="sendType email 1" name="id[1][email]" value="[email protected]">
<input type="checkbox" class="sendType text 1" name="id[1][text]" value="0412345678">
<span id="id[1][name]">Charlie Gery allen</span>
</li>
<li class="form-line">
<input type="checkbox" class="sendType all 6" name="id[6][all]" value="ye ere ertert">
<input type="checkbox" class="sendType email 6" name="id[6][email]" value="[email protected]">
<input type="checkbox" class="sendType text 6" name="id[6][text]" value="0415698721">
<span id="id[6][name]">ye ere ertert</span>
</li>
<li class="form-line">
<input type="checkbox" class="sendType all 7" name="id[7][all]" value="erterert utrtuuy 67678">
<input type="checkbox" class="sendType email 7" name="id[7][email]" value="[email protected]">
<input type="checkbox" class="sendType text 7" name="id[7][text]" value="0598746248">
<span id="id[7][name]">erterert utrtuuy 67678</span>
</li>
<li class="form-line">
<input type="checkbox" class="sendType all 8" name="id[8][all]" value="rrrrrrtyertertrrrrrr yyyyyyyetryeyyyyyyy">
<input type="checkbox" class="sendType email 8" name="id[8][email]" value="">
<input type="checkbox" class="sendType text 8" name="id[8][text]" value="">
<span id="id[8][name]">rrrrrrtyertertrrrrrr yyyyyyyetryeyyyyyyy</span>
</li>
<li class="form-line" id="id_2">
<button id="input_2" type="submit" class="form-submit-button">Send Message</button>
</li>
</ul>
如果任何人都可以得到它,感謝堆。
乾杯 查理
如果您使用更多的描述性變量名稱,生活會更加甜美。當你盯着sa,ct,ce,s2,s3等海洋時,很難建立一個程序流程的心理模型。爲什麼要成爲一個人類代碼縮減器? – 2011-02-11 05:44:36