-1
當我寫數組元素時,它不會顯示輸出,當我點擊發送和取消輸出將不會打印輸出將打印.... plese檢查我的查詢,如果它的錯誤...我認爲功能是停止工作,當我點擊按鈕,它沒有告訴我document.write不會顯示輸出
<html>
<TITLE>Javascript Arrays</TITLE>
<BODY bgcolor="#F0F0F0">
<h1>Implementation of Sort and Reverse Functions </h>
<h2>Enter number of Elements</h2>
<form name="Anonymous Ghost">
<INPUT type="number" name="takeinput" id="valueField" size="30">
<Input type="button" name="name1" value="Take Input" onClick="a()"> </form>
<form name="Anonymous Ghost">
<Input type="button" name="name2" value="Ascending" onClick="b()">
<Input type="button" name="name3" value="Descending" onClick="c()">
</form>
</BODY>
<SCRIPT>
function a() {
var val = parseInt(document.getElementById('valueField').value)
var words = new Array(val);
for (k = 0; k < words.length; k = k + 1) {
words[k] = window.prompt("Enter String # " + k, "");
}
}
function b() {
words.sort() ;
document.write("SORTED WORDS:" + "<BR>") ;
for (k = 0 ; k < words.length ; k = k + 1) {
document.write(words[ k ] + "<BR>") ;
}
}
function c() {
words.sort() ;
words.reverse() ;
document.write("SORTED WORDS:" + "<BR>") ;
for (k = 0 ; k < words.length ; k = k + 1) {
document.write(words[ k ] + "<BR>") ;
}
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
'words'是你的'a'函數的局部變量。它在'b'和'c'中都沒有定義。所以一個簡單的解決辦法是將'單詞'定義爲一個全局變量。 – Arnauld
你是否像這樣_really_格式化你的代碼?多可怕的一團糟! –
請格式化您的代碼。您的編輯應該有一個選項。 –