代码报错 需要找下原因 // 初始化 const start = document.getElementById("getstart").addEventListener("click", function () { let select_bu01 = 0; select_bu02 = 0; select_bu03 = 0; select_bu04 = 0; select_bu05 = 0; select_y01 = 0 select_y02 = 0 select_y03 = 0 select_y04 = 0 select_y05 = 0 select_y06 = 0 select_y07 = 0 select_y08 = 0 select_y09 = 0 select_y10 = 0 select_h01 = 0 select_h02 = 0 ifMDM = 0 let tmp_data = { MDM: ifMDM, BU01: select_bu01, BU02: select_bu02, BU03: select_bu03, BU04: select_bu04, BU05: select_bu05, Year1: select_y01, Year2: select_y02, Year3: select_y03, Year4: select_y04, Year5: select_y05, Year6: select_y06, Year7: select_y07, Year8: select_y08, Year9: select_y09, Year10: select_y10 }; let jsonString = JSON.stringify(tmp_data); console.log(jsonString); return jsonString; });
时间: 2024-04-22 18:24:21 浏览: 130
这段代码中的问题在于,你在点击事件的处理函数中使用了 `return` 语句来返回 `jsonString`。然而,`addEventListener` 方法并不期望处理函数返回值,所以无法正确处理这个返回值。
如果你想在点击事件处理函数执行完毕后获取 `jsonString` 的值,可以考虑将其保存到外部变量或者调用另一个函数来处理。
下面是修改后的代码示例:
```javascript
const start = document.getElementById("getstart").addEventListener("click", function () {
let select_bu01 = 0;
let select_bu02 = 0;
let select_bu03 = 0;
let select_bu04 = 0;
let select_bu05 = 0;
let select_y01 = 0;
let select_y02 = 0;
let select_y03 = 0;
let select_y04 = 0;
let select_y05 = 0;
let select_y06 = 0;
let select_y07 = 0;
let select_y08 = 0;
let select_y09 = 0;
let select_y10 = 0;
let select_h01 = 0;
let select_h02 = 0;
let ifMDM = 0;
let tmp_data = {
MDM: ifMDM,
BU01: select_bu01,
BU02: select_bu02,
BU03: select_bu03,
BU04: select_bu04,
BU05: select_bu05,
Year1: select_y01,
Year2: select_y02,
Year3: select_y03,
Year4: select_y04,
Year5: select_y05,
Year6: select_y06,
Year7: select_y07,
Year8: select_y08,
Year9: select_y09,
Year10: select_y10
};
let jsonString = JSON.stringify(tmp_data);
console.log(jsonString);
// 在这里可以使用 jsonString 或者调用其他函数来处理它
});
```
在上述示例中,我移除了 `return` 语句,并将 `jsonString` 的值保留在了点击事件处理函数的内部。你可以在 `console.log` 后面的注释处使用 `jsonString` 或者调用其他函数来处理它。
阅读全文