如何使动态下拉列表中的选定值保持选中状态
How to make the selected values in a dynamic drop down lists remain selected?
我有5个下拉列表和一个提交按钮。下拉列表的每个内容都依赖于前一个下拉列表的选择值。我想确保每个下拉列表的选定值保持选中,即使在提交按钮被点击后。有人能帮我一下吗?我看到一个网站,但做的是在ASP。. NET,我更喜欢用PHP, javascript
<script type="text/javascript">
function changeSelect(lang, num, valueElement, nameSelect, nameSpan, dataSup, endId){
var boxName = document.getElementById(nameSelect);
var hackIeSpan = document.getElementById(nameSpan);
var contentOption = $.ajax({
url: "select.php",
global: false,
type: "POST",
data: {
lang : lang,
num : num,
select : valueElement,
dataSup : dataSup,
endId : endId
},
dataType: "html",
async:false,
success: function(msg){
}
}
).responseText;
hackIeSpan.innerHTML = contentOption;
}
<form id="searchForm" method="post" action="search.php?search&page=0&lang=<?php echo $lang; ?>">
<fieldset>
<select name="sort" onchange="changeSelect('<?php echo $lang; ?>', 1, this.value, 'type_list', 'hackIeType', 'null', 'null'); return true;">
<option value="0"><?php echo trad('SEARCH_01', $lang); ?></option>
<option value="Rental"><?php echo trad('SEARCH_01a', $lang); ?></option>
<option value="Sale"><?php echo trad('SEARCH_01b', $lang); ?></option>
</select>
<span id="hackIeType">
<select name="type_list">
<option value="0"><?php echo trad('SEARCH_04', $lang); ?> ...</option>
</select>
</span>
.......
<span id="hackIeSleeps">
<select name="bedrooms">
<option value="0"><?php echo trad('SEARCH_05', $lang); ?> ...</option>
</select>
</span>
<span id="spanEndId">
<input id="endId" name="endId" type="hidden" value="" />
</span>
<input id="submit_search_home" type="submit" value="<?php echo trad('BTNSEARCH', $lang); ?>" />
</fieldset>
当您按下提交按钮时会发生什么?你最终会回到同一页吗?您只需要根据$_POST
中发送的值为所需选项提供selected="selected"
属性。
作为一个例子(这可能不适合你,取决于当你按下提交时到底发生了什么):
<form id="searchForm" method="post" action="search.php?search&page=0&lang=<?php echo $lang; ?>">
<fieldset>
<select name="sort" onchange="changeSelect('<?php echo $lang; ?>', 1, this.value, 'type_list', 'hackIeType', 'null', 'null'); return true;">
<option value="0"<?php if ($_POST['name'] === '0') echo ' selected="selected"'; ?>><?php echo trad('SEARCH_01', $lang); ?></option>
<option value="Rental"<?php if ($_POST['name'] == 'Rental') echo ' selected="selected"'; ?>><?php echo trad('SEARCH_01a', $lang); ?></option>
<option value="Sale"<?php if ($_POST['name'] == 'Sale') echo ' selected="selected"'; ?>><?php echo trad('SEARCH_01b', $lang); ?></option>
</select>
<span id="hackIeType">
<?php
if ($_POST['name'] === '0') {
// The first select value is default, so we can just output a standard select
// to be populated by AJAX
?>
<select name="type_list">
<option value="0"><?php echo trad('SEARCH_04', $lang); ?> ...</option>
</select>
<?php
} else {
// The first select is populated, so copy the logic that generates your second
// select from 'select.php' and put it here, then output it
}
?>
</span>
.......
<span id="hackIeSleeps">
<?php
if ($_POST['type_list'] === '0') {
// The second select value is default, so we can just output a standard select
// to be populated by AJAX
?>
<select name="bedrooms">
<option value="0"><?php echo trad('SEARCH_05', $lang); ?> ...</option>
</select>
<?php
} else {
// The second select is populated, so copy the logic that generates your third
// select from 'select.php' and put it here, then output it
}
?>
</span>
<span id="spanEndId">
<input id="endId" name="endId" type="hidden" value="" />
</span>
<input id="submit_search_home" type="submit" value="<?php echo trad('BTNSEARCH', $lang); ?>" />
</fieldset>
相关文章:
- 如何使bxslider仅在移动视图中处于活动状态
- 使用纯javascript而非jquery使所选选项卡处于活动状态并保持非活动状态
- 使url匹配'/''/索引'和'/index.html'到单一状态
- 如何使按钮具有 :活动状态 1 秒
- React.js:如何使内联样式在状态更改时自动更新进度条
- WordPress切换菜单 - 使菜单在当前页面上保持打开状态
- 使第二个选项卡处于第一个活动状态
- 使index.html的默认状态不带#something
- 如何使单选按钮在选择一个选项(CSS)后显示为选中状态
- 使选项卡处于活动/非活动状态未显示正确的数据
- 使用Angular UI Router,如何使一个状态成为所有状态的子状态
- 如何在Github电子原子中禁用或使浏览器窗口处于非活动状态
- 在益智游戏中,如何使可移动瓷砖的悬停为红色,而其他瓷砖保持原始状态
- 设置单元格值,使其在内联编辑中保持可编辑状态
- 使秒表在请求之间处于活动状态
- 如何在Adobe Acrobat中使JavaScript处于睡眠状态
- 单击关闭时,使引导下拉列表保持打开状态
- 使容器保持打开状态,以便首次单击每个导航项
- 使用服务跟踪用户状态并使 $watch() 正常工作
- 根据单选按钮的状态变化使儿童内容出现/消失