0

我无法掌握如何使用 jquery .trigger()。我只是在复选框周围循环并查找已选中的复选框。然后我试图将这些值映射到键值对。如果我记录 paymentValue 和 paymentName 我会得到正确的输出,类似于:cashtrue所以我想要像cash : true 这样的键值。除了当我尝试运行此代码时,我得到一个paymentName is not defined 错误

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
});

有任何想法吗?

4

1 回答 1

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')
});
于 2013-10-15T03:17:47.427 回答