JS动态生成select的option
方法一:
//1、JS获取控制器传入的参数值:
'papercharge' => json_encode($paper_charge),
//2、页面 JS
$('.paper_charge').empty();
var obj = <?php echo $papercharge;?>; //对象
var arr = [];
//对象 转数组
for(let i in obj) {
//arr.push (obj[i] ) //返回属性值
//arr.push (i) //返回键名
arr[i] = obj[i]
}
//重新给select初始化option
var obj = document.getElementById('paper_charge')
for (var i=0; i< arr.length;i++) {
obj.options.add(new Option(arr[i], i+1))
}
方法二:
//1、JS获取控制器传入的参数值:
'papercharge' => json_encode($paper_charge),
//2、页面 JS
$('.paper_charge').empty();
var obj = <?php echo $papercharge;?>; //对象
var arr_value = Object.values(obj)
var arr_keys = Object.keys(obj)
//根据需要对arr_value arr_keys处理