我无法掌握如何使用jquery .trigger()。我只是循环复选框并找到已选中的复选框。然后我尝试将这些值映射到键值对。如果我记录paymentValue和paymentName,我会得到正确的输出,例如:现金和 true ,所以我想要像 cash:true 这样的东西我的关键价值。 除非我尝试运行此代码,否则我会收到 paymentName未定义错误
var filterPayment = $('input[name="filter-payment"]:checked');
var paymentObj = {};
$(document).bind("updateFilters", function() {
filterPayment.each( function () {
var paymentValue = paymentObj[this.value];
paymentValue = this.checked;
var paymentName = userObj[this.name];
paymentName = this.value;
});
});
$(document).trigger('updateFilters', {
paymentValue : paymentName
});
有什么想法吗?
答案 0 :(得分:2)
尝试
var filterPayment = $('input[name="filter-payment"]');
var paymentObj = {};
var userObj = {};
//listern to the change event
filterPayment.change(function () {
var paymentValue = paymentObj[this.value];
paymentValue = this.checked;
var paymentName = userObj[this.name];
paymentName = this.value;
//create a parameter object, since the key is dynamic need to use bracket notation to assign the value
var param = {};
param[paymentValue] = paymentName;
$(document).trigger('updateFilters', param);
}).filter(':checked').change()
$(document).bind("updateFilters", function () {
alert('filter updated')
});