gpt4 book ai didi

javascript - 如何更改以下代码以不使用 eval?

转载 作者:行者123 更新时间:2023-12-03 16:41:56 26 4
gpt4 key购买 nike

var settings = {};

$(".js-gen-settings").each(function(){
var date;

if (($(this).attr("type") === "checkbox") || ($(this).attr("type") === "radio")){//if its a checkbox
eval("settings."+this.name+" = "+$(this).is(":checked"));
}else{
date = $(this).val();
if (date == ""){
eval("settings." + this.name + " = null");
}else{
eval("settings." + this.name + " = '" + date + "'");
}
}
});

JSON.stringify 版本的 settings 将使用 ajax 发送到服务器。

最佳答案

如果要动态修改对象的属性,只需将名称放在[括号]中即可。

settings[this.name] = null

settings[this.name] = date;

settings[this.name] = $(this).is(":checked");

关于javascript - 如何更改以下代码以不使用 eval?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7067370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com