【问题标题】:js document sort not working [duplicate]js文档排序不起作用[重复]
【发布时间】:2015-12-22 11:16:38
【问题描述】:

我正在努力学习 JavaScript,我正在关注 Michael Moncur JavaScript 的 24Hr 第 4 版。本书中的一个示例如下所示,假设对一组名称进行排序。但是当我点击按钮时什么都没有发生。代码是正确的还是书已经过时了。

名为 sort.html 的 HTML 文件

<html>
<head>
<title>Array Sorting Example</title>
<script type="text/javascript" language="javascript" src="sort.js">
</script>
</head>
<body>
<h1>Sorting String Arrays</h1>
<p>Enter two or more names in the field below,
and the sorted list of names will appear in the
text area.</p>
<form name="theform">
Name:
<input type="text" name="newname" size="20">
<input type="button" name="addname" value="Add"
onclick = "SortNames();">
<br>
<h2>Sorted Names</h2>
<textarea cols="60" rows="10" name = "sorted">
The sorted names will appear here.
</textarea>
</form>
</body>
</html>

这是 JS 文件名 sort.js

// initialize the counter and the array
var numnames=0;
var names = new Array();
function SortNames() {
// Get the name from the text field
thename=document.theform.newname.value;
// Add the name to the array
names[numnames]=thename;
// Increment the counter
numnames++;
// Sort the array
names.sort();
document.theform.sorted.value=names.join(“\n”);
}

任何想法有什么问题

【问题讨论】:

  • 在 24 小时内学习某事,很可能导致只制作需要 24 小时顶级的东西,除此之外,当然,如果你用 光速 学习 :)

标签: javascript html


【解决方案1】:
document.theform.sorted.value=names.join("\n");

问题在于您使用的是印刷双引号。

如果您将 替换为" 将起作用。

【讨论】:

  • 非常感谢我错过了这个。我做了 HTML 文档,但忘记了 JS 文档。
  • 如果您觉得此答案有帮助,请接受此答案。谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 2017-01-30
  • 1970-01-01
  • 2018-10-27
  • 2018-09-13
  • 1970-01-01
  • 2018-03-13
相关资源
最近更新 更多