如何绑定复选框以从javascript对象获取正确的数据?
我有一个对象,其中包含一些我希望在单击复选框时被访问的数据 以下是我的复选框示例如何绑定复选框以从javascript对象获取正确的数据?,javascript,jquery,object,checkbox,Javascript,Jquery,Object,Checkbox,我有一个对象,其中包含一些我希望在单击复选框时被访问的数据 以下是我的复选框示例 <input type="checkbox" id="dealership1" name="dealership1"> <input type="checkbox" id="dealership2" name="dealership2"> <input type="checkbox" id="dealership3" name="dealership3"> &l
<input type="checkbox" id="dealership1" name="dealership1">
<input type="checkbox" id="dealership2" name="dealership2">
<input type="checkbox" id="dealership3" name="dealership3">
<input type="checkbox" id="state1" name="state1">
<input type="checkbox" id="state2" name="state2">
<input type="checkbox" id="state3" name="state3">
$('input').change(函数(){
var type=$(this.attr('data-id');
var name=$(this.attr('name');
var n=单据[类型][名称]。名称;
});
请阅读如何创建带括号的文档[var1][var2]。名称可能重复的
var docs = {
'dealership': {
dealership1: {
"name" : "Trade-In Appraisal",
"image": "http://unsplash.it/100"
},
dealership2: {
"name" : "Service Details",
"image": "http://unsplash.it/200"
},
dealership3: {
"name" : "Referral Form",
"image": "http://unsplash.it/300"
},
},
'state': {
state1: {
"name" : "Application for Title",
"image": "http://unsplash.it/400"
},
state2: {
"name" : "Bill of Sale",
"image": "http://unsplash.it/500"
},
state3: {
"name" : "County of Title Issuance",
"image": "http://unsplash.it/600"
},
}
}
<input type="checkbox" data-id="dealership" name="dealership1">
<input type="checkbox" data-id="dealership" name="dealership2">
<input type="checkbox" data-id="dealership" name="dealership3">
<input type="checkbox" data-id="state" name="state1">
<input type="checkbox" data-id="state" name="state2">
<input type="checkbox" data-id="state" name="state3">
$('input').change(function() {
var type = $(this).attr('data-id');
var name = $(this).attr('name');
var n = docs[type][name].name;
});