如何在新加载的页面上执行(下拉)操作
How to make (drop-down) action occur on newly loaded page?
我有一个下拉菜单来选择使用的货币。做出选择后,页面将以所选货币重新加载。我希望所选货币的"title"标签显示在新打开的网页的文本区域中。我有在现有打开的页面中添加"title"标签的代码,但这并没有将其放在新加载页面的文本区域中。这可能吗?
如果可能,当用户刷新新网页时,文本区域中的内容将被记住/调用。
这是两个问题,知道一个问题的答案,请不要犹豫。
到目前为止我的代码:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<form name="currency select" title="Currency Selector">
<select name="currency" id="currencyList" onchange="window.document.location.href=this.options[this.selectedIndex].value;" value="GO">
<option value="" disabled="disabled" selected="selected" none="">$ € £</option>
<option value="/session/currency/usd/" title="US Dollar">USD</option>
<option value="/session/currency/eur/" title="EURO">EUR</option>
</select>
<textarea id="showTitle"></textarea>
</form>
</body>
<script>
$(document).ready(function()
{
$(document).on('change','#currencyList',function()
{
var result = $("option:selected",this).attr('title');
$("#showTitle").text(result);
});
});
</script>
我终于弄清楚了问题是什么,这是由于直接附加到下拉菜单上的onchange
脚本重定向页面(我现在已经注释掉了)。下拉列表更改时,会话数据也没有更新。试试这段代码:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<form name="currency select" title="Currency Selector">
<select name="currency" id="currencyList" value="GO">
<option value="" disabled="disabled" selected="selected" none="">$ € £</option>
<option value="/session/currency/usd/" title="US Dollar">USD</option>
<option value="/session/currency/eur/" title="EURO">EUR</option>
</select>
<textarea id="showTitle"></textarea>
</form>
<script>
$(document).ready(function(){
var field = document.getElementById("showTitle");
var savedValue = sessionStorage.getItem("autosave");
// See if we have an autosave value
// (this will only happen if the page is accidentally refreshed)
if (savedValue) {
// Restore the contents of the text field
field.value = savedValue;
//set dropdown to session value
$('#currencyList option[title="'+ savedValue + '"]').prop('selected', true);
}
// Listen for changes in the text field
field.addEventListener("input", function() {
// And save the results into the session storage object
sessionStorage.setItem("autosave", this.value);
});
$(document).on('change','#currencyList',function(){
//get text
var result = $("option:selected",this).attr('title');
//set field
$(field).val(result);
//set session storage item
sessionStorage.setItem("autosave", result);
//redirect
window.document.location.href=this.options[this.selectedIndex].value;
});
});
</script>
</body>
</html>
如果要
在新页面中显示选项的标题,则应将其发送到服务器。
服务器获取该值并呈现它,当您的新页面加载时,它带有文本区域上的值。
编辑:
您必须重新组织代码才能使用 sessionStorage。
<select name="currency" id="currencyList" value="GO">
<option value="" disabled="disabled" selected="selected" none="">$ € £</option>
<option value="/session/currency/usd/" title="US Dollar">USD</option>
<option value="/session/currency/eur/" title="EURO">EUR</option>
</select>
<textarea id="showTitle"></textarea>
$(document).ready(function() {
var field = document.getElementById("showTitle");
// See if we have an autosave value
// (this will only happen if the page is accidentally refreshed)
if (sessionStorage.getItem("autosave")) {
// Restore the contents of the text field
field.value = sessionStorage.getItem("autosave");
}
// Listen for changes in the text field
field.addEventListener("change", function() {
// And save the results into the session storage object
sessionStorage.setItem("autosave", field.value);
});
$(document).on('change', '#currencyList', function() {
var result = $("option:selected", this).attr('title');
$("#showTitle").text(result);
window.document.location.href = $("option:selected", this).val();
});
});
新版本
的代码,它也没有做它应该做的事情;文本区域在新加载的网页上保持空白。
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<form name="currency select" title="Currency Selector">
<select name="currency" id="currencyList" onchange="window.document.location.href=this.options[this.selectedIndex].value;" value="GO">
<option value="" disabled="disabled" selected="selected" none="">$ € £</option>
<option value="/session/currency/usd/" title="US Dollar">USD</option>
<option value="/session/currency/eur/" title="EURO">EUR</option>
</select>
<textarea id="showTitle"></textarea>
</form>
</body>
<script>
$(document).ready(function()
{
$(document).on('change','#currencyList',function()
{
var result = $("option:selected",this).attr('title');
$("#showTitle").text(result);
});
// Get the text field that we're going to track
var field = document.getElementById("showTitle");
// See if we have an autosave value
// (this will only happen if the page is accidentally refreshed)
if (sessionStorage.getItem("autosave")) {
// Restore the contents of the text field
field.value = sessionStorage.getItem("autosave");
}
// Listen for changes in the text field
field.addEventListener("change", function() {
// And save the results into the session storage object
sessionStorage.setItem("autosave", field.value);
});
});
</script>
这是最终有效的代码:
编辑
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<form name="currency select" title="Currency Selector">
<select name="currency" id="currencyList" onchange="window.document.location.href=this.options[this.selectedIndex].value;" value="GO">
<option value="" disabled="disabled" selected="selected" none="">TEST</option>
<option value="/session/currency/usd/" title="US Dollar">USD</option>
<option value="/session/currency/eur/" title="EURO">EUR</option>
</select>
<textarea id="showTitle"></textarea>
</form>
<script>
$(document).ready(function(){
var field = document.getElementById("showTitle");
var savedValue = sessionStorage.getItem("autosave");
// See if we have an autosave value
// (this will only happen if the page is accidentally refreshed)
if (savedValue) {
// Restore the contents of the text field
field.value = savedValue;
//set dropdown to session value
$('#currencyList option[title="'+ savedValue + '"]').prop('selected', true);
}
// Listen for changes in the text field
field.addEventListener("input", function() {
// And save the results into the session storage object
sessionStorage.setItem("autosave", this.value);
});
$(document).on('change','#currencyList',function(){
//get text
var result = $("option:selected",this).attr('title');
//set field
$(field).val(result);
//set session storage item
sessionStorage.setItem("autosave", result);
//redirect
//window.document.location.href=this.options[this.selectedIndex].value;
});
});
</script>
</body>
相关文章:
- 下拉选择可自动更改第二个下拉选择
- 如何使用jQuery选择下拉列表的值
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 使用Ajax或JavaScript选择下拉列表选项后,在同一页面上执行PHP脚本
- 如何使用文本下拉列表中的绑定值来执行计算
- 如何使用angular js验证器执行下拉控件验证
- 如何在新加载的页面上执行(下拉)操作
- MVC为不同的下拉列表选择执行不同的方法
- 对下拉菜单中的特定选项执行onclick验证
- 单击以执行操作上的引导下拉按钮
- 当从下拉列表中选择项目时,执行javascript代码
- 如何在每次用户选择一个特定的值到一个选择下拉菜单时使用jQuery执行一个函数?
- 使用javascript函数在url执行下拉选项/按钮
- 如何在执行内联编辑时在数据表中显示包含值的下拉列表
- 如何从下拉列表中存储值以在其他php页面中使用而不执行操作
- 从XML填充下拉列表-代码执行顺序错误
- 在SelectedIndexChanged事件的下拉列表上执行javascript confirm
- 在打开下拉列表之前执行
- 使javascript基于下拉菜单中的输入执行一个操作
- 我如何编写一个函数,只有当用户与UI中的下拉菜单交互时才会执行,而不是从后端更新模型