我有以下trial.html Code.It有<select>
标记。
PS:它没有重复,因为它没有使用Ajax,我不熟悉Ajax,并且有像我这样的人。
<!DOCTYPE html>
<html>
<head>
<title>Popup</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script src="trial.js"></script>
</head>
<body>
<h1></h1>
<div role="main">
<form >
<label for="timeframe">Remove all browsing data from:</label>
<select id="timeframe" name="timeframe">
<option value="Null">Please Select one</option>
<option value="hour">the past hour</option>
<option value="day">the past day</option>
<option value="week">the past week</option>
<option value="4weeks">the past four weeks</option>
<option value="forever">the beginning of time</option>
</select>
<input type="submit" id="submit" value="submit">
</form>
</div>
</body>
</html>
这是trial.js
$(document).ready(
function(){
var selected;
$("#timeframe").change(
function(){
selected=$("#timeframe option:selected").val();
})
///Figure out how to use selected outside.
//function that gives time taking the selecte as input
parseMilliseconds= function (timeframe) {
var now = new Date().getTime();
var milliseconds = {
'hour': 60 * 60 * 1000,
'day': 24 * 60 * 60 * 1000,
'week': 7 * 24 * 60 * 60 * 1000,
'4weeks': 4 * 7 * 24 * 60 * 60 * 1000
};
if (milliseconds[timeframe])
return now - milliseconds[timeframe];
if (timeframe === 'forever')
return 0;
return null;
},
if(selected!=undefined){
//that deletes history
chrome.browsingData.remove(
{ "since" : parseMilliseconds(selected) }, {
"appcache": true,
"cache": true,
"cookies": true,
"downloads": true,
"fileSystems": true,
"formData": true,
"history": true,
"indexedDB": true,
"localStorage": true,
"serverBoundCertificates": true,
"pluginData": true,
"passwords": true,
"webSQL": true
}
, function callback(){
alert("Data Deleted");
})
}
}
)
所以我想在change事件上使用在回调函数之外选择的变量。我该怎么做?