[英]Passing variable with Javascript action
If anyone can help with this, it would be greatly appreciated! 如果有人可以帮助您,将不胜感激!
I have an HTML form, with a drop down for the state you're in. 我有一个HTML表单,其中包含您所处的状态的下拉列表。
Then I have a JavaScript function that determines the state picked, and redirects the user accordingly. 然后,我有一个JavaScript函数,该函数确定选择的状态,并相应地重定向用户。
function pickstate()
{
if(document.drop_list.State.value == "AR" ){
document.drop_list.action = "www.theurl.com";
document.drop_list.submit();
}
}
My question is: can I use this method (and if so, how) to pass the state to the next form, so that when the user is redirected, the dropdown says the state they picked. 我的问题是:我可以使用这种方法(如果可以的话,如何使用)将状态传递给下一个表单,以便在重定向用户时,下拉列表显示他们选择的状态。
I know how to do this using PHP and POST, but this is not posting, it's just a redirect to the correct URL based on the state, and since 20 or so states will use the same form, I don't want to make the user select their state again. 我知道如何使用PHP和POST来执行此操作,但这不是发布,它只是基于状态的重定向到正确的URL,并且由于20个左右的状态将使用相同的形式,因此我不想将用户再次选择其状态。
The reason I'm using this method is not everyone will be redirected — many states will stay on the form that is first displayed.. This is why I can't use POST. 我使用这种方法的原因不是每个人都会被重定向-许多州将停留在最初显示的表单上。这就是为什么我不能使用POST的原因。
Help! 救命! Thank you.
谢谢。
You can add a hash to the URL 您可以将哈希添加到URL
function pickstate()
{
var stateVal = document.drop_list.State.value;
switch(stateVal) {
case "AR"
document.drop_list.action = "www.theurl.com/#"+stateVal;
break;
case "IT"
document.drop_list.action = "www.theurl.com/#"+stateVal;
break;
}
document.drop_list.submit();
}
Then the next form can select the state by using the passed hash 然后,下一个表单可以使用传递的哈希值选择状态
window.location.hash; // eg. will be equal to #AR or #IT
You could store the value in local storage. 您可以将值存储在本地存储中。
function pickState() {
if (document.drop_list.State.value == "AR") {
document.drop_list.action = "www.theurl.com";
localStorage.setItem('selectedState', 'AR');
document.drop_list.submit();
}
}
Then add another functions that sets the value. 然后添加另一个设置值的函数。
function setState() {
var storedSelectedState = localStorage.getItem('selectedState');
if (storedSelectedState) {
document.drop_list.State.value = storedSelectedState;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.