如何使用下拉菜单调用 jQuery 函数,以便下拉菜单的 onChange 刷新 div,而不仅仅是将函数结果添加到其中?
How to call a jQuery function with a dropdown so that the dropdown's onChange will refresh the div, not just add the function result to it?
背景
我给出这个背景是为了防止我以错误的方式思考问题,这很可能是我的想法。我正在尝试实现一个时区下拉列表,然后运行此 jQuery 代码(
https://github.com/shonihei/weekly-scheduler-component ) 在目标 div 上。所有代码都在 HTML 文件中。这是一个相当长的函数,它创建一个 div 的网格,其中 headers 列作为星期几,headers 行作为 1-hour-block 开始时间,并使用户能够到 select div。我不想将所有代码粘贴到此处,因此假设它在我的 <script>
标签内。我集成背后的使用场景是:
- 我有特定的时间。例如。下午 1 点到下午 5 点 MST。我只显示那些时间的方块。
- 用户select从下拉列表中选择了他们的时区。例如。太平洋标准时间
- 根据他们的下拉列表 selection,我的时间在调度程序中转换为他们的时区,并且更新显示,以便我的时间显示在他们的时间中。例如。他们看到中午 12 点到下午 4 点的街区。
- 他们可以 select 在下拉列表中选择不同的时区,并获得该时区的更新时间表 table。
我在尝试中遇到了很多问题。一个是我在每次下拉更改时显示一个新的 table,导致页面在 table 之后显示 table。另一个是我真的不明白下拉菜单应该如何调用 jQuery 来用新日历刷新目标 div。这个问题是为了了解我应该如何使用创建此调度程序的 jQuery 函数来考虑此下拉列表,并了解它是如何正确完成的。我搜索了如何重置或清空 div,但我不确定这是否真的是我需要的。我是jQuery新手
代码
这是 HTML,其中 jQuery 不起作用。
<label for="timezone_selector">Please select a time zone:</label>
<select id="timezone_selector"
required="required"
onChange="$('#target').weekly_schedule()">
<option value="" selected="selected" hidden="hidden">Click here</option>
<option value="gmt-7">Pacific, PDT (Daylight Savings) GMT-7</option>
<option value="gmt-8">Pacific, PST (Standard) GMT-8</option>
<option value="gmt-6">Mountain, MDT (Daylight Savings) GMT-6</option>
<option value="gmt-7">Mountain, MST (Standard) GMT-7</option>
<option value="gmt-5">Central, CDT (Daylight Savings) GMT-5</option>
<option value="gmt-6">Central, CST (Standard) GMT-6</option>
<option value="gmt-4">Eastern, EDT (Daylight Savings) GMT-4</option>
<option value="gmt-5">Eastern, EST (Standard) GMT-5</option>
</select>
<div class="container" style="margin: auto; width: 50%; height: 75%;
display: flex; flex-direction: row; justify-content: center;">
<div id="target">
</div>
</div>
我也试过了
<script>
$("#timezone_selector").change(function() {
$('#target').weekly_schedule();
});
</script>
正如@MichaelChon 在概念上建议的那样,我尝试在下拉列表更改时首先删除 table,我发现我可以使用 .empty()
<script>
$("#timezone_selector").change(function() {
$('#target').empty();
$('#target').weekly_schedule();
});
</script>
看起来效果不错。
背景
我给出这个背景是为了防止我以错误的方式思考问题,这很可能是我的想法。我正在尝试实现一个时区下拉列表,然后运行此 jQuery 代码(
https://github.com/shonihei/weekly-scheduler-component ) 在目标 div 上。所有代码都在 HTML 文件中。这是一个相当长的函数,它创建一个 div 的网格,其中 headers 列作为星期几,headers 行作为 1-hour-block 开始时间,并使用户能够到 select div。我不想将所有代码粘贴到此处,因此假设它在我的 <script>
标签内。我集成背后的使用场景是:
- 我有特定的时间。例如。下午 1 点到下午 5 点 MST。我只显示那些时间的方块。
- 用户select从下拉列表中选择了他们的时区。例如。太平洋标准时间
- 根据他们的下拉列表 selection,我的时间在调度程序中转换为他们的时区,并且更新显示,以便我的时间显示在他们的时间中。例如。他们看到中午 12 点到下午 4 点的街区。
- 他们可以 select 在下拉列表中选择不同的时区,并获得该时区的更新时间表 table。
我在尝试中遇到了很多问题。一个是我在每次下拉更改时显示一个新的 table,导致页面在 table 之后显示 table。另一个是我真的不明白下拉菜单应该如何调用 jQuery 来用新日历刷新目标 div。这个问题是为了了解我应该如何使用创建此调度程序的 jQuery 函数来考虑此下拉列表,并了解它是如何正确完成的。我搜索了如何重置或清空 div,但我不确定这是否真的是我需要的。我是jQuery新手
代码
这是 HTML,其中 jQuery 不起作用。
<label for="timezone_selector">Please select a time zone:</label>
<select id="timezone_selector"
required="required"
onChange="$('#target').weekly_schedule()">
<option value="" selected="selected" hidden="hidden">Click here</option>
<option value="gmt-7">Pacific, PDT (Daylight Savings) GMT-7</option>
<option value="gmt-8">Pacific, PST (Standard) GMT-8</option>
<option value="gmt-6">Mountain, MDT (Daylight Savings) GMT-6</option>
<option value="gmt-7">Mountain, MST (Standard) GMT-7</option>
<option value="gmt-5">Central, CDT (Daylight Savings) GMT-5</option>
<option value="gmt-6">Central, CST (Standard) GMT-6</option>
<option value="gmt-4">Eastern, EDT (Daylight Savings) GMT-4</option>
<option value="gmt-5">Eastern, EST (Standard) GMT-5</option>
</select>
<div class="container" style="margin: auto; width: 50%; height: 75%;
display: flex; flex-direction: row; justify-content: center;">
<div id="target">
</div>
</div>
我也试过了
<script>
$("#timezone_selector").change(function() {
$('#target').weekly_schedule();
});
</script>
正如@MichaelChon 在概念上建议的那样,我尝试在下拉列表更改时首先删除 table,我发现我可以使用 .empty()
<script>
$("#timezone_selector").change(function() {
$('#target').empty();
$('#target').weekly_schedule();
});
</script>
看起来效果不错。