Maintain Selected Values in Dropdown Menu Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!What are valid values for the id attribute in HTML?Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?How can I know which radio button is selected via jQuery?How can I get query string values in JavaScript?How can I select an element with multiple classes in jQuery?Get selected value in dropdown list using JavaScript?Get selected text from a drop-down list (select box) using jQueryHow to style a <select> dropdown with only CSS?How to send FormData objects with Ajax-requests in jQuery?jQuery Get Selected Option From Dropdown
Was Kant an Intuitionist about mathematical objects?
Most effective melee weapons for arboreal combat? (pre-gunpowder technology)
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
Did pre-Columbian Americans know the spherical shape of the Earth?
Is there hard evidence that the grant peer review system performs significantly better than random?
As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?
Where is the Next Backup Size entry on iOS 12?
Relating to the President and obstruction, were Mueller's conclusions preordained?
Special flights
What is the "studentd" process?
Why is a lens darker than other ones when applying the same settings?
A proverb that is used to imply that you have unexpectedly faced a big problem
What does it mean that physics no longer uses mechanical models to describe phenomena?
AppleTVs create a chatty alternate WiFi network
GDP with Intermediate Production
How to ask rejected full-time candidates to apply to teach individual courses?
What would you call this weird metallic apparatus that allows you to lift people?
Moving a wrapfig vertically to encroach partially on a subsection title
Why datecode is SO IMPORTANT to chip manufacturers?
Is it dangerous to install hacking tools on my private linux machine?
Tips to organize LaTeX presentations for a semester
A term for a woman complaining about things/begging in a cute/childish way
One-one communication
In musical terms, what properties are varied by the human voice to produce different words / syllables?
Maintain Selected Values in Dropdown Menu
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!What are valid values for the id attribute in HTML?Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?How can I know which radio button is selected via jQuery?How can I get query string values in JavaScript?How can I select an element with multiple classes in jQuery?Get selected value in dropdown list using JavaScript?Get selected text from a drop-down list (select box) using jQueryHow to style a <select> dropdown with only CSS?How to send FormData objects with Ajax-requests in jQuery?jQuery Get Selected Option From Dropdown
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a cascading dropdown menu populated by an AJAX call which works as intended. However, when I submit the form and refresh the page, the selected values reset. I'd like to be able to maintain the selected values within their drop-down boxes upon submitting the form and refreshing the page.
I've found several solutions on Stack Overflow already, but can't seem to find one that shows how to maintain the selected values from a form who's options are dynamically populated.
My HTML and JS are below.
<form method="get" class='d-print-none' id='gameForm' team-ids-url=% url 'ajax_load_teamids' % game-ids-url=% url 'ajax_load_gameids' % novalidate>
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid"></select>
<select class="custom-select" id="inputGameId" name="inputgameid"></select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function()
var url = $("#gameForm").attr("team-ids-url");
$.ajax(
url: url,
success: function(data)
$("#inputTeamId").html(data);
);
);
$("#inputTeamId").change(function()
var url = $("#gameForm").attr("game-ids-url");
var selected_teamid = $(this).val();
localStorage.setItem("SelectedTeamId", selected_teamid);
$.ajax(
url: url,
data:
'selected_teamid':selected_teamid
,
success: function (data)
$("#inputGameId").html(data);
)
)
</script>
javascript jquery html ajax
add a comment |
I have a cascading dropdown menu populated by an AJAX call which works as intended. However, when I submit the form and refresh the page, the selected values reset. I'd like to be able to maintain the selected values within their drop-down boxes upon submitting the form and refreshing the page.
I've found several solutions on Stack Overflow already, but can't seem to find one that shows how to maintain the selected values from a form who's options are dynamically populated.
My HTML and JS are below.
<form method="get" class='d-print-none' id='gameForm' team-ids-url=% url 'ajax_load_teamids' % game-ids-url=% url 'ajax_load_gameids' % novalidate>
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid"></select>
<select class="custom-select" id="inputGameId" name="inputgameid"></select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function()
var url = $("#gameForm").attr("team-ids-url");
$.ajax(
url: url,
success: function(data)
$("#inputTeamId").html(data);
);
);
$("#inputTeamId").change(function()
var url = $("#gameForm").attr("game-ids-url");
var selected_teamid = $(this).val();
localStorage.setItem("SelectedTeamId", selected_teamid);
$.ajax(
url: url,
data:
'selected_teamid':selected_teamid
,
success: function (data)
$("#inputGameId").html(data);
)
)
</script>
javascript jquery html ajax
«when I submit the form and refresh the page...» -- Look for localStorage.
– Louys Patrice Bessette
Mar 8 at 23:35
When you submit the form, all selected values are going to be lost as they are intended to be transferred via HTTP. You would need either to store the values locally or collect them from where ever they are stored (SQL) and populate the fields when the page loads. If you're using Server Side scripting, this script can do this for you. If not, you will need to collect the values and populate the form with jQuery.
– Twisty
Mar 9 at 0:23
add a comment |
I have a cascading dropdown menu populated by an AJAX call which works as intended. However, when I submit the form and refresh the page, the selected values reset. I'd like to be able to maintain the selected values within their drop-down boxes upon submitting the form and refreshing the page.
I've found several solutions on Stack Overflow already, but can't seem to find one that shows how to maintain the selected values from a form who's options are dynamically populated.
My HTML and JS are below.
<form method="get" class='d-print-none' id='gameForm' team-ids-url=% url 'ajax_load_teamids' % game-ids-url=% url 'ajax_load_gameids' % novalidate>
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid"></select>
<select class="custom-select" id="inputGameId" name="inputgameid"></select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function()
var url = $("#gameForm").attr("team-ids-url");
$.ajax(
url: url,
success: function(data)
$("#inputTeamId").html(data);
);
);
$("#inputTeamId").change(function()
var url = $("#gameForm").attr("game-ids-url");
var selected_teamid = $(this).val();
localStorage.setItem("SelectedTeamId", selected_teamid);
$.ajax(
url: url,
data:
'selected_teamid':selected_teamid
,
success: function (data)
$("#inputGameId").html(data);
)
)
</script>
javascript jquery html ajax
I have a cascading dropdown menu populated by an AJAX call which works as intended. However, when I submit the form and refresh the page, the selected values reset. I'd like to be able to maintain the selected values within their drop-down boxes upon submitting the form and refreshing the page.
I've found several solutions on Stack Overflow already, but can't seem to find one that shows how to maintain the selected values from a form who's options are dynamically populated.
My HTML and JS are below.
<form method="get" class='d-print-none' id='gameForm' team-ids-url=% url 'ajax_load_teamids' % game-ids-url=% url 'ajax_load_gameids' % novalidate>
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid"></select>
<select class="custom-select" id="inputGameId" name="inputgameid"></select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function()
var url = $("#gameForm").attr("team-ids-url");
$.ajax(
url: url,
success: function(data)
$("#inputTeamId").html(data);
);
);
$("#inputTeamId").change(function()
var url = $("#gameForm").attr("game-ids-url");
var selected_teamid = $(this).val();
localStorage.setItem("SelectedTeamId", selected_teamid);
$.ajax(
url: url,
data:
'selected_teamid':selected_teamid
,
success: function (data)
$("#inputGameId").html(data);
)
)
</script>
javascript jquery html ajax
javascript jquery html ajax
asked Mar 8 at 23:23
George RodmanGeorge Rodman
5817
5817
«when I submit the form and refresh the page...» -- Look for localStorage.
– Louys Patrice Bessette
Mar 8 at 23:35
When you submit the form, all selected values are going to be lost as they are intended to be transferred via HTTP. You would need either to store the values locally or collect them from where ever they are stored (SQL) and populate the fields when the page loads. If you're using Server Side scripting, this script can do this for you. If not, you will need to collect the values and populate the form with jQuery.
– Twisty
Mar 9 at 0:23
add a comment |
«when I submit the form and refresh the page...» -- Look for localStorage.
– Louys Patrice Bessette
Mar 8 at 23:35
When you submit the form, all selected values are going to be lost as they are intended to be transferred via HTTP. You would need either to store the values locally or collect them from where ever they are stored (SQL) and populate the fields when the page loads. If you're using Server Side scripting, this script can do this for you. If not, you will need to collect the values and populate the form with jQuery.
– Twisty
Mar 9 at 0:23
«when I submit the form and refresh the page...» -- Look for localStorage.
– Louys Patrice Bessette
Mar 8 at 23:35
«when I submit the form and refresh the page...» -- Look for localStorage.
– Louys Patrice Bessette
Mar 8 at 23:35
When you submit the form, all selected values are going to be lost as they are intended to be transferred via HTTP. You would need either to store the values locally or collect them from where ever they are stored (SQL) and populate the fields when the page loads. If you're using Server Side scripting, this script can do this for you. If not, you will need to collect the values and populate the form with jQuery.
– Twisty
Mar 9 at 0:23
When you submit the form, all selected values are going to be lost as they are intended to be transferred via HTTP. You would need either to store the values locally or collect them from where ever they are stored (SQL) and populate the fields when the page loads. If you're using Server Side scripting, this script can do this for you. If not, you will need to collect the values and populate the form with jQuery.
– Twisty
Mar 9 at 0:23
add a comment |
1 Answer
1
active
oldest
votes
Consider the following code: https://jsfiddle.net/Twisty/5p2yg034/
HTML
<form method="get" class="d-print-none" id="gameForm" data-team-url="./teams" gdata-game-url="./games">
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid">
<option></option>
<option value="t1">Team 1</option>
<option value="t2">Team 2</option>
<option value="t3">Team 3</option>
</select>
<select class="custom-select" id="inputGameId" name="inputgameid">
<option></option>
<option value="g1">Game 1</option>
<option value="g2">Game 2</option>
<option value="g3">Game 3</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
</div>
</form>
One thing to look at here is using data
attributes so that you can later use .data()
in jQuery.
JavaScript
$(function()
function loadData()
var fd = localStorage.getItem("fd");
if (fd != null)
return JSON.parse(fd);
return false;
function saveData(fd)
localStorage.setItem("fd", JSON.stringify(fd));
function get(item)
if (item == undefined)
item = "team"
var url;
switch (item)
case "team":
url = $("#gameForm").data("team-url");
$.get(url, function(r)
$("#inputTeamId").val(r);
);
break;
case "game":
url = $("#gameForm").data("game-url");
$.get(url,
'selected_teamid': $("#inputTeamId").val()
, function(r)
$("#inputGameId").val(r);
);
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
function loading(e)
e.preventDefault();
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
// Loading script
function init()
var formData = loadData();
if (formData)
$.each(formData, function(i, d)
$("#" + i).val(d);
);
else
get("team");
$("#inputTeamId").change(function()
get("team");
);
$("#gameForm").submit(loading);
init();
);
This example makes use of localStorage
to quickly store the form values. The functions can easily be adjusted to use Cookies or pushing them to the server to be stored. This depends on your needs.
If the form has basic data, collecting it in to an object will be easy and helpful for storing. We can only store String data to localStorage
, so we can use JSON.stringify()
to convert the object into a String that we can parse later.
When the page loads, we can attempt to load the data. If there is no localStorage
content, it will try to get a Team. If there is data, it will set that data in the form elements by ID.
In your script, you can collect the URLs using $("#gameForm").data('team-url')
and $("#gameForm").data('game-url')
. If your form gets more complex, you may consider using GET the data from one Script with a item passed to it.
Hope that helps.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072354%2fmaintain-selected-values-in-dropdown-menu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Consider the following code: https://jsfiddle.net/Twisty/5p2yg034/
HTML
<form method="get" class="d-print-none" id="gameForm" data-team-url="./teams" gdata-game-url="./games">
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid">
<option></option>
<option value="t1">Team 1</option>
<option value="t2">Team 2</option>
<option value="t3">Team 3</option>
</select>
<select class="custom-select" id="inputGameId" name="inputgameid">
<option></option>
<option value="g1">Game 1</option>
<option value="g2">Game 2</option>
<option value="g3">Game 3</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
</div>
</form>
One thing to look at here is using data
attributes so that you can later use .data()
in jQuery.
JavaScript
$(function()
function loadData()
var fd = localStorage.getItem("fd");
if (fd != null)
return JSON.parse(fd);
return false;
function saveData(fd)
localStorage.setItem("fd", JSON.stringify(fd));
function get(item)
if (item == undefined)
item = "team"
var url;
switch (item)
case "team":
url = $("#gameForm").data("team-url");
$.get(url, function(r)
$("#inputTeamId").val(r);
);
break;
case "game":
url = $("#gameForm").data("game-url");
$.get(url,
'selected_teamid': $("#inputTeamId").val()
, function(r)
$("#inputGameId").val(r);
);
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
function loading(e)
e.preventDefault();
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
// Loading script
function init()
var formData = loadData();
if (formData)
$.each(formData, function(i, d)
$("#" + i).val(d);
);
else
get("team");
$("#inputTeamId").change(function()
get("team");
);
$("#gameForm").submit(loading);
init();
);
This example makes use of localStorage
to quickly store the form values. The functions can easily be adjusted to use Cookies or pushing them to the server to be stored. This depends on your needs.
If the form has basic data, collecting it in to an object will be easy and helpful for storing. We can only store String data to localStorage
, so we can use JSON.stringify()
to convert the object into a String that we can parse later.
When the page loads, we can attempt to load the data. If there is no localStorage
content, it will try to get a Team. If there is data, it will set that data in the form elements by ID.
In your script, you can collect the URLs using $("#gameForm").data('team-url')
and $("#gameForm").data('game-url')
. If your form gets more complex, you may consider using GET the data from one Script with a item passed to it.
Hope that helps.
add a comment |
Consider the following code: https://jsfiddle.net/Twisty/5p2yg034/
HTML
<form method="get" class="d-print-none" id="gameForm" data-team-url="./teams" gdata-game-url="./games">
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid">
<option></option>
<option value="t1">Team 1</option>
<option value="t2">Team 2</option>
<option value="t3">Team 3</option>
</select>
<select class="custom-select" id="inputGameId" name="inputgameid">
<option></option>
<option value="g1">Game 1</option>
<option value="g2">Game 2</option>
<option value="g3">Game 3</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
</div>
</form>
One thing to look at here is using data
attributes so that you can later use .data()
in jQuery.
JavaScript
$(function()
function loadData()
var fd = localStorage.getItem("fd");
if (fd != null)
return JSON.parse(fd);
return false;
function saveData(fd)
localStorage.setItem("fd", JSON.stringify(fd));
function get(item)
if (item == undefined)
item = "team"
var url;
switch (item)
case "team":
url = $("#gameForm").data("team-url");
$.get(url, function(r)
$("#inputTeamId").val(r);
);
break;
case "game":
url = $("#gameForm").data("game-url");
$.get(url,
'selected_teamid': $("#inputTeamId").val()
, function(r)
$("#inputGameId").val(r);
);
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
function loading(e)
e.preventDefault();
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
// Loading script
function init()
var formData = loadData();
if (formData)
$.each(formData, function(i, d)
$("#" + i).val(d);
);
else
get("team");
$("#inputTeamId").change(function()
get("team");
);
$("#gameForm").submit(loading);
init();
);
This example makes use of localStorage
to quickly store the form values. The functions can easily be adjusted to use Cookies or pushing them to the server to be stored. This depends on your needs.
If the form has basic data, collecting it in to an object will be easy and helpful for storing. We can only store String data to localStorage
, so we can use JSON.stringify()
to convert the object into a String that we can parse later.
When the page loads, we can attempt to load the data. If there is no localStorage
content, it will try to get a Team. If there is data, it will set that data in the form elements by ID.
In your script, you can collect the URLs using $("#gameForm").data('team-url')
and $("#gameForm").data('game-url')
. If your form gets more complex, you may consider using GET the data from one Script with a item passed to it.
Hope that helps.
add a comment |
Consider the following code: https://jsfiddle.net/Twisty/5p2yg034/
HTML
<form method="get" class="d-print-none" id="gameForm" data-team-url="./teams" gdata-game-url="./games">
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid">
<option></option>
<option value="t1">Team 1</option>
<option value="t2">Team 2</option>
<option value="t3">Team 3</option>
</select>
<select class="custom-select" id="inputGameId" name="inputgameid">
<option></option>
<option value="g1">Game 1</option>
<option value="g2">Game 2</option>
<option value="g3">Game 3</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
</div>
</form>
One thing to look at here is using data
attributes so that you can later use .data()
in jQuery.
JavaScript
$(function()
function loadData()
var fd = localStorage.getItem("fd");
if (fd != null)
return JSON.parse(fd);
return false;
function saveData(fd)
localStorage.setItem("fd", JSON.stringify(fd));
function get(item)
if (item == undefined)
item = "team"
var url;
switch (item)
case "team":
url = $("#gameForm").data("team-url");
$.get(url, function(r)
$("#inputTeamId").val(r);
);
break;
case "game":
url = $("#gameForm").data("game-url");
$.get(url,
'selected_teamid': $("#inputTeamId").val()
, function(r)
$("#inputGameId").val(r);
);
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
function loading(e)
e.preventDefault();
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
// Loading script
function init()
var formData = loadData();
if (formData)
$.each(formData, function(i, d)
$("#" + i).val(d);
);
else
get("team");
$("#inputTeamId").change(function()
get("team");
);
$("#gameForm").submit(loading);
init();
);
This example makes use of localStorage
to quickly store the form values. The functions can easily be adjusted to use Cookies or pushing them to the server to be stored. This depends on your needs.
If the form has basic data, collecting it in to an object will be easy and helpful for storing. We can only store String data to localStorage
, so we can use JSON.stringify()
to convert the object into a String that we can parse later.
When the page loads, we can attempt to load the data. If there is no localStorage
content, it will try to get a Team. If there is data, it will set that data in the form elements by ID.
In your script, you can collect the URLs using $("#gameForm").data('team-url')
and $("#gameForm").data('game-url')
. If your form gets more complex, you may consider using GET the data from one Script with a item passed to it.
Hope that helps.
Consider the following code: https://jsfiddle.net/Twisty/5p2yg034/
HTML
<form method="get" class="d-print-none" id="gameForm" data-team-url="./teams" gdata-game-url="./games">
<div class="input-group">
<select class="custom-select" id="inputTeamId" name="inputteamid">
<option></option>
<option value="t1">Team 1</option>
<option value="t2">Team 2</option>
<option value="t3">Team 3</option>
</select>
<select class="custom-select" id="inputGameId" name="inputgameid">
<option></option>
<option value="g1">Game 1</option>
<option value="g2">Game 2</option>
<option value="g3">Game 3</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" value="submit" onclick="loading();">Run</button>
</div>
</div>
</form>
One thing to look at here is using data
attributes so that you can later use .data()
in jQuery.
JavaScript
$(function()
function loadData()
var fd = localStorage.getItem("fd");
if (fd != null)
return JSON.parse(fd);
return false;
function saveData(fd)
localStorage.setItem("fd", JSON.stringify(fd));
function get(item)
if (item == undefined)
item = "team"
var url;
switch (item)
case "team":
url = $("#gameForm").data("team-url");
$.get(url, function(r)
$("#inputTeamId").val(r);
);
break;
case "game":
url = $("#gameForm").data("game-url");
$.get(url,
'selected_teamid': $("#inputTeamId").val()
, function(r)
$("#inputGameId").val(r);
);
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
function loading(e)
e.preventDefault();
saveData(
'inputTeamId': $("#inputTeamId").val(),
'inputGameId': $("#inputGameId").val()
);
// Loading script
function init()
var formData = loadData();
if (formData)
$.each(formData, function(i, d)
$("#" + i).val(d);
);
else
get("team");
$("#inputTeamId").change(function()
get("team");
);
$("#gameForm").submit(loading);
init();
);
This example makes use of localStorage
to quickly store the form values. The functions can easily be adjusted to use Cookies or pushing them to the server to be stored. This depends on your needs.
If the form has basic data, collecting it in to an object will be easy and helpful for storing. We can only store String data to localStorage
, so we can use JSON.stringify()
to convert the object into a String that we can parse later.
When the page loads, we can attempt to load the data. If there is no localStorage
content, it will try to get a Team. If there is data, it will set that data in the form elements by ID.
In your script, you can collect the URLs using $("#gameForm").data('team-url')
and $("#gameForm").data('game-url')
. If your form gets more complex, you may consider using GET the data from one Script with a item passed to it.
Hope that helps.
answered Mar 9 at 2:38
TwistyTwisty
14.5k11635
14.5k11635
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072354%2fmaintain-selected-values-in-dropdown-menu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
«when I submit the form and refresh the page...» -- Look for localStorage.
– Louys Patrice Bessette
Mar 8 at 23:35
When you submit the form, all selected values are going to be lost as they are intended to be transferred via HTTP. You would need either to store the values locally or collect them from where ever they are stored (SQL) and populate the fields when the page loads. If you're using Server Side scripting, this script can do this for you. If not, you will need to collect the values and populate the form with jQuery.
– Twisty
Mar 9 at 0:23