我正在开发以前开发的功能,并且代码未优化。
这是代码:
function myFunc(response) {
setChekBoxValue($("#test1"), response.test1);
setChekBoxValue($("#test2"), response.test2);
setTextBoxValue($("#test3"), response.test3);
setTextBoxValue($("#test4"), response.test4);
setChekBoxValue($("#test5"), response.test5);
setRadioButtonValue("test6", response.test6);
setRadioButtonValue("test7", response.test7);
}
响应是我得到的所有c#类属性及其值。
但是问题是类属性超过100,这就是为什么set函数被写100次的原因,这对我来说似乎不合适。
因此,我需要帮助的是如何检查jQuery或JavaScript中响应的特定属性的类型。
例如“ test1”的类型为“ boolean”,“ test3”的类型为“ string”。
如果获得属性的类型,则可以基于该属性使用开关盒。
谢谢。
最佳答案
javascript for .. in ..
循环遍历键。要获取元素,请执行以下操作:
var obj = {"a":1, "b":2};
for(var key in obj) {
var element = obj[key];
console.log(element);
}
这给出1和2。