我正在使用Jquery iCheck样式化输入字段,但我到了一个无法处理iCheck回调的地步。我想要检索的是,所有在给定类名的容器内被选中的输入值。
http://jsfiddle.net/lgtsfiddler/E4bKg/1/ 这里是我一直在尝试的代码,但是点击的元素在流程中不会被推入数组,只有在第二次点击时才会。
<ul class="brands">
<li>
<input class ="marken" type="checkbox" name="brand" value="brand1">
<label>brand1</label>
</li>
<li>
<input class ="marken" type="checkbox" name="brand" value="brand1">
<label>brand2</label>
</li>
<li>
<input class ="marken" type="checkbox" name="brand" value="brand1">
<label>brand3</label>
</li>
<li>
<input class ="marken" type="checkbox" name="brand" value="brand1">
<label>brand4</label>
</li>
</ul>
代码:
$(document).ready(function () {
//here I style with iCheck
$('ul.brands input').each(function () {
var self = $(this),
label = self.next(),
label_text = label.text();
label.remove();
self.iCheck({
checkboxClass: 'icheckbox_line-blue',
radioClass: 'iradio_line',
insert: '<div class="icheck_line-icon"></div>' + label_text
});
});
//Register click event
$('ul.brands input').on('ifClicked', function (event) {
brands();
});
});
//here I try to retrieve all checked values
function brands() {
var brands = [];
$('ul.brands li div.icheckbox_line-blue').each(function (index, value) {
var attr_class = $(value).attr('class');
console.log(attr_class);
//check if icheckbox_line-blue class contains checked
if (attr_class.indexOf("checked") !== -1) {
brands.push($(value).find('input').val());
}
});
console.log(brands);
}
那么如何在选择后检查这个类呢?
checked
属性吗?似乎不是这个问题。is(:checked)
返回true,但当表单提交时,复选框并没有被发送为已选中。 - Alexander Suraphel