【发布时间】:2019-11-21 20:25:12
【问题描述】:
我在 Laravel 中运行了一个 foreach 循环,其中有一个下拉菜单。下拉列表应该告知人们某个调查的状态(已发送、未发送、已接收等)。这个想法是某个调查的状态只是存储在本地。因此,即使在页面刷新后,状态也会被记住。
@foreach($surveys as $sur)
<tr>
<td style="padding:5px 25px 0 0;">{{$sur->titel}}</td>
<td style="padding:5px 25px 0 0;">
<select id="myDynamicSelectBox">
<option>None</option>
<option>Not send</option>
<option>Sent</option>
<option>Answer received</option>
</select>
</td>
@endforeach
我的 JavaScript 如下所示:
const mySel = document.getElementById("myDynamicSelectBox");
mySel.addEventListener("change",function() {
localStorage.setItem("selValue",this.value); // save it
});
let val = localStorage.getItem("selValue");
if (val) mySel.value=val; // set the dropdown
// trigger the change in case there are other events on the select
mySel.onchange();
</script>
所以我面临的问题是它确实保存了 foreach 循环中第一个调查的状态。但由于某种原因,其他调查无法保持这些值。有什么想法吗?
感谢您的宝贵时间,
干杯,
【问题讨论】:
-
id="myDynamicSelectBox" id 对于每个选择器应该是唯一的
-
so.. 我应该将 id 更改为 class 之类的其他名称吗?
标签: javascript laravel-5 foreach local-storage