我有一个JavaScript对象,看起来像这样.
var myObj = [
{
"HOLIDAY": {
"Sun": "Date",
"Mon": "Date",
"Tue": "Date",
"Wed": "Date",
"Thr": "Date",
"Fri": "Date",
"Sat": "Date"
}
}
]
Run Code Online (Sandbox Code Playgroud)
我有一些HTML代码,看起来像这样
<input data-event="change"
data-variable="myObj"
data-bind="[0]['HOLIDAY']['Sun']"
type="text">
Run Code Online (Sandbox Code Playgroud)
在HTML上,如果我对该字段进行任何更改,我已经存储了要修改的JavaScript变量.我编写了类似这样的JavaScript代码.
$(document).on('change', '[data-event= change]', function(){
//get-variable-name where to bind data
//Get object location inside the variable
var sVarName = $(this).data('variable');
var sObjLoca = $(this).data('bind');
eval(sVarName+sObjLoca +' = ' $(this).val());
});
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来解决这个问题,目前我正在使用eval()我不想使用的方法,因为许多元素都会有'change'事件而show'eval'会影响我的代码的性能.