gpt4 book ai didi

javascript - 将 Jquery 转换为 Javascript 尝试失败

转载 作者:行者123 更新时间:2023-12-03 01:00:08 25 4
gpt4 key购买 nike

我在将这段 Jquery 代码转换为纯 JavaScript 时遇到困难。

var model = sessionStorage.getItem('themodel');
$('#select0').change(function () {
var modelValue = $(this).val();
sessionStorage.setItem('themodel', modelValue );
})
.val(model)

这是我的尝试

var model = sessionStorage.getItem('themodel');
document.getElementById('select0').options.change(function () {
var modelValue = this.value;
sessionStorage.setItem('themodel', modelValue );
})
document.getElementById('select0').options.value = model;

最佳答案

您需要在select而不是options上监听并设置值

var model = sessionStorage.getItem('themodel');
document.getElementById('select0').change(function () {
var modelValue = this.value;
sessionStorage.setItem('themodel', modelValue );
})
document.getElementById('select0').value = model;

关于javascript - 将 Jquery 转换为 Javascript 尝试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52658342/

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