gpt4 book ai didi

jQuery iCheck 回调

转载 作者:行者123 更新时间:2023-12-01 00:03:01 25 4
gpt4 key购买 nike

我正在使用 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);
}

那么检查完之后如何检查这个类呢?

最佳答案

您可以检查原始复选框,并从父级获取按钮描述,而不是检查类。

旁注,我使用 ifToggled 事件,因此对于选中和未选中的元素都会触发该事件。

代码:

$('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
});
});

$('ul.brands input').on('ifToggled', function (event) {
brands();
});

function brands() {
var brands = [];
$('ul.brands input').each(function (index, value) {
if ($(this).is(':checked')) {
brands.push($(this).parent().text());
}
});
console.log(brands);
}

演示:http://jsfiddle.net/IrvinDominin/S2NdY/

关于jQuery iCheck 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20287802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com