gpt4 book ai didi

javascript - 如何使用 JavaScript 获取 AJAX 的选择值

转载 作者:行者123 更新时间:2023-12-02 23:25:16 25 4
gpt4 key购买 nike

我有 2 个 HTML 下拉菜单。

<div class="bar-option">
<label>Filter:</label>
<form>
<select name="retreat_locations" onchange="filterRetreats(this.value, 'count(reviews.retreat_id)')">
<option value="alllocations" selected="selected">All Locations</option>
<?php foreach ($this->locations as $location) {?>
<option value="<?=htmlentities($location->retreat_location);?>">
<?=htmlentities($location->retreat_location);?> </option>
<?php }?>
</select>
</form>
</div>
<div class="bar-option">
<label>Sort by:</label>
<select onchange="filterRetreats('alllocations', this.value)">
<option selected='selected' value='retreat_number_of_reviews'>Popularity</option>
<option value='retreat_founded_in'>Age</option>
<option value='total_review_cost'>Cost</option>
</select>
</div>

从上面可以看出,第二个 select 使用静态值“alllocations”作为 onchange 函数的第一个参数。

如何用第一个下拉列表 (retreat_locations) 的选定选项替换此静态值?

我尝试了retreat_locations.value,但没有成功。

感谢任何帮助。

最佳答案

您可以使用:

document.querySelector('[name=retreat_locations]').value

您的代码将变成:

filterRetreats(document.querySelector('[name=retreat_locations]').value, this.value)

演示:

function filterRetreats(value1, value2) {
console.log(value1, value2);
}
<div class="bar-option">
<label>Filter:</label>
<form>
<select name="retreat_locations" onchange="filterRetreats(this.value, 'count(reviews.retreat_id)')">
<option value="alllocations" selected="selected">All Locations</option>
<option value="2">2</option>
</select>
</form>
</div>
<div class="bar-option">
<label>Sort by:</label>
<select onchange="filterRetreats(document.querySelector('[name=retreat_locations]').value, this.value)">
<option selected='selected' value='retreat_number_of_reviews'>Popularity</option>
<option value='retreat_founded_in'>Age</option>
<option value='total_review_cost'>Cost</option>
</select>
</div>

其他 -old school- 选项是将 id 标记添加到第一个选择,例如id="retreat_locations" 并使用 document.getElementById('retreat_locations').value

关于javascript - 如何使用 JavaScript 获取 AJAX 的选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56761089/

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