当我将变量放在函数外部时,为什么javascript会返回NaN?

时间:2017-02-12 00:11:14

标签: javascript

当我将这些可变数据放在函数之外时,你们知道whyn吗?结果是"不是数字"?

Dont变量应该变成" Global"当放置一个功能?

<html>
<head>
<script type="text/javascript">
// V-LOXY > Variaveis Calculo de Frete

var Comp = document.getElementById('ComprimenTo').value;
var Larg = document.getElementById('Largura').value;
var Alt = document.getElementById('Altura').value;
var TxCubagem = 300

function PesoCubado(){

document.getElementById('PesoCub').innerHTML = ((Comp/100)*(Larg/100)*(Alt/100)*TxCubagem).toFixed(1)+' kg';
}

</script>

</head>

<body>

<label>Comprimento [cm]:
<input id='ComprimenTo' type="number" width="15"/></label><br />
<p></p>
<label>Largura [cm]:
<input id='Largura' type="number" width="15"/></label><br />
<p></p>
<label>Altura [cm]:
<input id='Altura' type="number" width="15"/></label><br />
<p></p>       
<input type='button' onclick='PesoCubado()' value="Calcular" >
<p></p>

<p>Peso Cubado: <b id='PesoCub'></b> </p>


</body>

</html>

2 个答案:

答案 0 :(得分:0)

您需要做两件事:将您的脚本移动到您尝试处理的元素下方(在正文结束前),并在{{1}期间获取输入的value函数调用:

PesoCubado()

答案 1 :(得分:0)

您的脚本遇到了几个问题:

  • 您的变量指向文本字段的初始值,因此不会使用最新值。您应该获得//Setting File-Count to 0 this.fileCount = 0; //Init new Dictionary var allTracks = new Dictionary<string, Dictionary<string, List<PlaylistTrack>>>(); //Getting Directory where Genre-Files are DirectoryInfo dir = new DirectoryInfo(this.path); int count = 0; //Looping through files in this Directory #1 foreach (var file in dir.GetFiles("*.txt")) { //Get only Genre-Files, not Track-Lists if (file.Name != "My Tracks.txt" && file.Name != "Tracks.txt") { count++; } } //Init new string Array that will hold the Links to my Playlist for every genre this.playlistLinks = new string[count]; //Looping through files in this Directory #2 foreach (var file in dir.GetFiles("*.txt")) { //Get only Genre-Files, not Track-Lists if(file.Name != "My Tracks.txt" && file.Name != "Tracks.txt") { //If the Genre-File has content if (File.ReadAllText(file.FullName) != "") { //Getting the Genre Name, by splitting the Genre-File-Name string name = file.Name.Split(new string[] { ".txt" }, StringSplitOptions.None)[0]; //Reading the Genre-File using (StreamReader sr = new StreamReader(file.FullName)) { string line = ""; bool first = false; //If the new line has content while ((line = sr.ReadLine()) != null) { //If it's not the first line and the line is not "" if (first && line != "") { //Splitting line to get Data //split[0] = PlaylistID; split[1] = OwnerID string[] split = line.Split(new string[] { " // " }, StringSplitOptions.None); //Getting Playlist Tracks and storing them in the Dictionary //HERE HAPPENS THE ERROR allTracks[name][split[0]] = this.getPlaylistTracks(split[1], split[0]); //Looping through the Playlist-Tracks within the Dictionary foreach (PlaylistTrack tr in allTracks [name][split[0]]) { //If the Track is on Spotify's Servers if (!tr.IsLocal) { //Filtering (not important here -> does work) if (tr.AddedAt > time) { //Creating a string that holds Track's info string write = tr.Track.Name + " // " + string.Join(",", tr.Track.Artists.Select(source => source.Name)); //Filtering tracks that i haven't listened to, by checking if track exists in "My Tracks.txt" //(not important here -> does work) if (!this.foundTrack(write)) { //Adding collected Tracks to another List, sorted by Genre //So all filtered Tracks from all Playlists within a genre are stored in one list this.tracksToAdd[this.fileCount].Add(tr); } } } } } //If it's the first line where the line is not "" if(!first && line != "") { string[] split = line.Split(new string[] { " // " }, StringSplitOptions.None); //Getting the PlaylistID of my Playlist this.playlistLinks[this.fileCount] = split[0]; first = true; } } } } //Increasing on every new Genre-File this.fileCount++; } } 函数中每个字段的值。
  • 从输入返回的值始终是字符串,因此,您必须使用PesoCubado函数将它们转换为数字。
  • 您最好将代码包装在Number上的load事件监听器中,以确保您的页面完全加载。

希望这会有所帮助。

汤姆