简体   繁体   中英

Can anyone tell me what is wrong in my code

what I did is I made 3 functions in js file which is being used by 2 html5 pages. In the two functions I am storing the values of the selected items in global variables ie from dropdown lists and want to display it in another page...but var returns a null value.

 <!DOCTYPE html5>
<html>
<head>
<script type="text/javascript" src="123.js"></script>
</head>

<body>
<form>
Select your favorite browser:
<select id="city_list" onchange="city()">
  <option></option>
  <option>Google Chrome</option>
  <option>Firefox</option>  
  <option>Internet Explorer</option>
  <option>Safari</option>
  <option>Opera</option>
</select>
<select id="blood_group" onchange="bloodGroup()">
  <option></option>
  <option>Google Chrome</option>
  <option>Firefox</option>  
  <option>Internet Explorer</option>
  <option>Safari</option>
  <option>Opera</option>
</select>


</form>
</body>

</html>

File 2

 <!DOCTYPE html5>
<html>
<head>
<script type="text/javascript" src="123.js"></script>
</head>

<body onload="answers()">
<form>

<p>Your favorite browser is: <input type="text" id="favorite2" size="20"></p>
<p>Your favorite browser is: <input type="text" id="favorite3" size="20"></p>

</form>
</body>

</html>

Javascript i use:

var bloodtype;
var whichcity;
function city()
{
var mylist=document.getElementById("city_list");
whichcity=mylist.options[mylist.selectedIndex].text;
document.getElementById("favorite").value=whichcity;
var selectedanswer=document.getElementById("city_list").selectedIndex;
}

function bloodGroup()
{
var mylist=document.getElementById("blood_group");
bloodtype=mylist.options[mylist.selectedIndex].text;
}

function getvalues()
{
document.getElementById("favorite2").value=whichcity;
document.getElementById("favorite3").value=bloodtype;
}

What you are trying to do is sharing data between two independent html documents. This is impossible without persistent storage since html itself has no concept of persistence. Try the following:

Option 1: Get yourself a server, upload the files there and use a <form> to send the data to the server (for example Apache )

Option 2: Read more about local storage , a browser feature in HTML5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM